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

221
Visualizações
React : state has different value depending on where you ask

I have a list of buttons that change a state, a string (used to decide if a modal is displayed ans as the title for this modal). What should happen :

    1. if the modal is not displayed, clicking on any of these buttons should change open the modal and set its title.
    1. if the modal is displayed and the same button that opened it is clicked again, the modal should close (title cleared).
    1. if the modal is displayed and another button is clicked, the modal should stay but its title should change (title changed).

Points 1 and 3 work fine. But 2 is not. So I decided to put some console.logs in there. If I log inside a useEffect linked to my state : title state changes on each button click that should change it but it's never cleared.

So I logged inside the handler function (triggered when a button is clicked) :

  • title state always appears as an empty string, even if there's a title inside the modal.

So there it is, it doesn't make any sense to me. Here's parts of my code if it helps :

  const [logsModalTitle, setLogsModalTitle] = useState("");
...
  const handlingLogs = (inputClause) => {
    console.log("handling logs : ", showLogsModal, logsModalTitle);
    if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
      setLogsModalTitle("");
    } else {
      setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
    }
  };
...
  <MyCustomButton
                buttonInnerText="ERROR"
                buttonSize="medium"
                buttonActionFunctionOne={handlingLogs}
                buttonActionPropOne={input.clauses[i]}
              />
...
  useEffect(() => {
    console.log(logsModalTitle);
  }, [logsModalTitle]);

Any idea on what is causing this problem ?

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

0

in my opinion it is really strange code, but this may work:

const handlingLogs = (inputClause) => {
  if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
    setLogsModalTitle("");
    setShowLogsModal(false);
  } else {
    setShowLogsModal(true);
    setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
  }
};

and you can use just one state as showLogsModal = logsModalTitle !== "";

const [logsModalTitle, setLogsModalTitle] = useState("");

const handlingLogs = (inputClause) => {
  if (`${inputClause.name}(${inputClause.label})` === logsModalTitle) {
    setLogsModalTitle("");
  } else {
    setLogsModalTitle(`${inputClause.name}(${inputClause.label})`);
  }
};

const showLogsModal = logsModalTitle !== "";

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