Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

66
Visualizações
Logical AND (&&) and OR (||) operators confusion

I'm still getting confused with the logical operator. My case is that I'm using React.js component such as below:

const Component = () => {
   const [data, setData] = useState({
      date: "",
      value: "",
   })

   const [disabled, setDisabled] = useState(true);

  useEffect(() => {
    if (data.date !== "" && data.value !== "") {
      setDisabled(false);
    } else if (data.date === "" || data.value === "") {
      setDisabled(true);
    }
  }, [data.date, data.value]);

  return <Button disabled={disabled} />
}

So my case here is that I have a state consisting of date and value where I receive an input from a user. If the input for one of the state (date or value) is empty, I want the button to be disabled. From my understanding, the logical operator should be correct. What happens right now is that the Button will be enabled once both input has been filled. However, if I emptied one of the field, it doesn't get disabled. Only when I empty both input field then Button would get disabled. What am I doing wrong here? Thank you so much in advance

UPDATE How I'm changing the state is by:

const handleDate = (_, dateString) => {
   setData({...data, date: dateString});
}

<DatePicker onChange={handleDate} />
<InputNumber onChange={(value) => {setData({...data, value: value})} />

*Note: I'm using Ant Design Input and Datepicker

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could set if not having date or not having value.

setDisabled(!data.date || !data.value);
about 4 years ago · Juan Pablo Isaza Relatório

0

Instead of using

else if

use

useEffect(() => {
if (data.date !== "" && data.value !== "") {
  setDisabled(false);
} else {
  setDisabled(true);
}

}, [data.date, data.value]);

I assume you are using useState correctly whilek changing date and value. Do keep us updated ;-)

about 4 years ago · Juan Pablo Isaza Relatório

0

Or means at least one of them. AND means both simultaneously.

So your OR (data.date === "" || data.value === "") means when at least one of them is empty it is true; therefore disabled. And your AND (data.date !== "" && data.value !== "") means if both of them are non empty simultaneously then true, in every other case false.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda