React, setState with async updater parameter?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In React class components, setState accepts either an object or an updater function, but that updater function is not meant to be async. The updater must stay synchronous and pure because React uses it to calculate the next state from the previous state and props, not to run asynchronous side effects.
The Correct Updater Pattern
When the next state depends on previous state, use the functional updater form:
This function runs synchronously from React’s perspective. It receives the previous state and returns the next partial state object.
Why an async Updater Is the Wrong Tool
It is tempting to write something like this:
That is not the intended contract. An async function returns a promise, but setState expects the updater to return an object or null, not a promise that resolves later. Even if the code type-checks loosely in some setups, it is conceptually wrong and leads to confusing behavior.
The updater function should be treated as pure state calculation, not as an asynchronous workflow hook.
Do the Async Work Outside setState
If you need asynchronous data, perform it first, then call setState with the result:
This keeps the responsibilities clear:
- async work happens in an event handler or lifecycle method
- state calculation stays synchronous inside
setState
Combine Previous State with Async Results Safely
Sometimes you need both asynchronous data and previous state. In that case, await first, then use the functional updater:
That is the right composition. The async part fetches the external value. The updater part calculates the next state from the previous state.
Remember That setState Is Scheduled
Another source of confusion is the phrase “setState is async”. That does not mean the updater function should be async. It means React schedules the state update rather than mutating this.state immediately in place.
If you need code to run after the state update has been applied in a class component, use the second callback parameter:
That callback is for post-update side effects. It is still separate from the updater function itself.
Hooks Are Different, but the Rule Still Applies
Newer React code often uses hooks instead of class components, but the same principle holds. The updater passed to setCount in hooks should also be synchronous:
Async work belongs around state updates, not inside the updater calculation.
Common Pitfalls
- Marking the
setStateupdater function asasyncand expecting React to await it. - Doing fetches or timers inside the updater instead of in an event handler, effect, or lifecycle method.
- Forgetting to use the functional updater when the next state depends on previous state.
- Misunderstanding “
setStateis asynchronous” as meaning the updater function itself should return a promise. - Mixing too many responsibilities into one
setStatecall and making state flow hard to reason about.
Summary
- The
setStateupdater function should be synchronous and pure. - Do asynchronous work before calling
setState, not inside the updater. - Use the functional updater when the next state depends on previous state.
- Use the second
setStatecallback for post-update side effects in class components. - Treat async workflows and state calculation as separate steps for predictable React code.
Related reading
- Read asynchronously data from NetworkStream with huge amount of packets
- Read file in EventMachine asynchronously
- Read Locks and Write Locks
- Read only first item from IAsyncEnumerable, then cancel
- React/RCTBridgeModule.h file not found
- Read environment variables in Node.js
- Reader/Writer Locks in C
- ReaderWriterLock vs lock
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.