"Why can't I access an updated state value in React immediately?"
@asidorenko_35K followers
142,252 views
9 Comments
Comments from the original post on X (Twitter)

@asidorenko_
Because it's not how React works. Updating a state in React triggers a new render (React calls your component function again with a new state). You can access the updated state variable during this new render.
53 likes

@Technoknol
Other solution is to use callback function like:
25 likes

@PineapplesDev
There’s a callback on setState if you need it immediately
10 likes

@BTamerhoulet
State updates are asynchronous
7 likes

@HarpalJadeja11
So what's the solution?
3 likes

@Babloo_o_
Closure
3 likes

@EberechiUche1
React batches changes and updates only after the whole function is done running. Hence a log immediately after changing set only had access to the initial state.
3 likes

@nadim_hallaq
What are you using to create this awesome code animation?
2 likes

@kazzkiq
Nice content mate, but it never ceases to amaze me how weirdly architected React is.
2 likes