Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Why can not I access an updated state value in React immediately?

Perhaps the most confusing or talked about topic in the React world is state management. And one of the most common questions that I see is why an updated state value in React can’t be accessed immediately.

I recently came across this tweet by Alex Sidorenko where he explains the phenomenon in a sleek-looking animation. So, I thought of sharing it here.

Essentially, the reason why you can not access an updated state value in React immediately is that React batches the state updates. So, when you call the setState() function, React re-renders the component and calls your component function again with a new state.

You can access the updated state value in the next render cycle. But the usual rendering of the component will happen just like how you might expect.

👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I do and want me to continue doing the same, I'd like you consider leaving a tip. I'd highly appreciate that. Cheers!