I have seted the value inside the function using useState, in same function I have accessed the set value but it doesn’t give me the updated value. If I access the set value outside function it gives the updated value. why useState have this behavior and how I can get updated value in in same function where I set value?
export default function App() {
const [value, setValue] = useState(1);
const myFunction= ()=>{
setValue(2);
console.log(value) //it gives me 1, but I need 2 as I set in above line
}
//but if I access the value outside myfunction it gives 2
console.log(value) // it gives 2
return(
....
....
)
}
3
Answers
When the console.log in your myFunction is called still the
value=1
. Even though you’ve set it before the updated value is not passed to the log. Console.log before the return runs when the setValue is called(rerender) At the time thevalue=2
.You can use useRef to get updated values without rerenders. Use State values(variables) only for showing UI changes.
useState
will change the value on the next render, not immediately. If you need to use the value in some other function either listen for value changes with an useEffect, or store the new value in another variable:Just to give a bit more explanation:
The value returned by useState is stored as a regular variable. When you create an instance of
myFunction
, it gets tied to the closure of that specific render, and to that specificvalue
variable.For your case, where you call
setValue
and then need to do something with that value, PhantomSpooks’ answer is spot on: either use a local variable to store and distribute the updated value, or move that logic touseEffect
(usual useEffect caveats apply in that case; it may be asynchronous and may be executed nie times).The generic way to circumvent this limitation of
useState
is to use a ref, as Chari proposes, though not as written in the answer; you should replace state with ref, as setting the ref won’t trigger re-render.You may use a ref as secondary storage, like that:
As you can see,
useRef
can be tricky, so it’s usually best to stick to the previous solution, either the local variable oruseEffect
, depending od the semantics of what you are doing.