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

162
Views
Set state twice in onChange ReactJS

I have the following snippet of React that I can't get to work right. Essentially I'm displaying a spinner based on the state of this onChange call (due to the update sometimes taking 3-5 seconds). However, the first call to set state seems to go unnoticed (or as I'm researching being batched up) and therefore the state of loading is never updated. This is part of a toggle/switch a user can select infinite times on a page (if they desired obviously).

  const [state, setState] = useState({ loading: false });  
  const onToggleChange = () => {
    setState(prevState => {
      return {
        ...prevState,
        loading: true,
      };
    });
    console.log("loading1: " + JSON.stringify(state));

    // this takes a long time depending on the users actions
    setFilterMode(!filterChecked);

    setState(prevState => {
      return {
        ...prevState,
        loading: false,
      };
    });
    console.log("loading2: " + JSON.stringify(state));
  };

I've reviewed the current questions/answer (which is how I got as far as the above), but it still doesn't load correctly.

about 4 years ago · Juan Pablo Isaza
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!