Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!