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

310
Views
How to fix issue with state not equal to current state

I am calling a function that uses state but the state that is being used in function is old state even though if you use the react debugger on chrome it displays new state.

const [somevalue,setsomevalue] = useState("newstate")

    function myfunction()
    {
       console.log(somevalue)
    }

myfunction()

output:

oldstate

I was thinking of using forceUpdate() but I am using classless component.

const CauseCodeChangeHandler = (e) => {
  
      setSelectedCauseCode(e.target.value);
  };

const serQtyChangeHandler = (e) =>
  {
    
    if (SelectedCauseCode == "CUS")
    {
       
     
    }
    else if (SelectedCauseCode == "OPS")
    {
    }
    else if (SelectedCauseCode == 'OOW')
    {
      
    }
  }

my component:

<ServiceSelectInput changeHandler={CauseCodeChangeHandler}/> 

  

It is using old CauseCode not one that I just selected.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is the stale closure problem and is something that useCallback is designed to address:

import {useCallback, useState} from 'react';

function Component () {
  const [someValue, setSomeValue] = useState('newstate');

  // This will make sure that `myFunction` is recreated
  // every time that `someValue` changes
  const myFunction = useCallback(() => console.log(someValue), [someValue]);

  // ...rest of component
}

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!