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

278
Visualizações
React useState setter is not working on a boolean value

I tried updating a state value using its setter from the useState hook, but it wouldn't update. The same setter is working in a different function while setting the value true.

To make sure that the function call was proper, I tried updating a different state from the the faultering function and it works!

I have been whacking my head as to why that particular state is not updating to a boolean false value.

const initialFormState = {
  0: { a: null, b: 0.0, c: 0.0 }
};
const [form, setForm] = useState(initialFormState);
const [fileUploadModal, setFileUploadModal] = useState(false);

function openFileInput() {
  setFileUploadModal(true);  // this works fine
}

function closeFileInput() {
  setFileUploadModal(false);   // this doesn't update
  setForm(initialFormState);   // this works fine as well
  console.log(fileUploadModal, form);
}

To sanity check:

useEffect(() => {
  console.log('File open state modified', fileUploadModal);
}, [fileUploadModal]);

This is not updated proving that there is some issue with the state setter.

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

0

So sorry for the question, but the problem was due to a template issue, the click also triggered the function which sets the value to true. The confusion arose because the state update useEffect was not called, and that lead me to different line of investigation. The real issue was the btn click was not propagated properly.

Thanks for the help everyone!

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