← BYTES
React.js·Intermediate·Ref: 4.2.19

Why does calling a state setter (e.g. from `useState`) not update the variable's value immediately, synchronously, in the very next line of code after calling it?

Have a question about this video? Mention reference 4.2.19 when you contact us.

a.It's simply a bug in React that hasn't been fixed
b.It does update immediately; this premise is false
c.State setters only work inside `async` functions
d.React schedules the update and re-renders the component with the new value on a subsequent render -- the current render's variables are a snapshot that doesn't change mid-render
Calling a setter schedules a re-render rather than mutating anything in place. The variable you have in your current closure is a fixed snapshot for that render; the updated value only becomes visible once the component actually re-renders and runs again with the new state.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES4219#Reactjs