React dispatch a method in async call
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In React, dispatching during an async operation is normal, but the async work should not live inside the reducer itself. The reducer stays pure and synchronous, while an event handler, effect, or thunk-like layer performs the request and dispatches actions around it. Once you keep that separation clear, async state management becomes much easier to reason about.
Keep the Reducer Pure
A reducer should only compute the next state from the current state and an action. It should not call fetch, set timers, or trigger other side effects.
A common useReducer setup looks like this.
The reducer is only describing state transitions. That is exactly where it should stop.
Dispatch Around the Async Call
The async function lives in the component, where it can dispatch a start action before the request and a success or error action afterward.
This is the core pattern for plain React state management with asynchronous work.
Use useEffect for Async Work Triggered by State or Props
If the request should happen automatically when the component mounts or when an input changes, use an effect. The dispatch pattern stays the same.
The cancellation flag prevents a late dispatch from updating state after the component is gone.
Plain React Dispatch Is Not Redux Thunk Dispatch
This is where many questions come from. In plain useReducer, dispatch expects an action object. It does not automatically know how to execute an async function or promise. If you pass a function directly, React will not treat it like Redux thunk middleware does.
So the correct question is not “how do I dispatch an async method”. The correct question is “where should the async method live, and which actions should it dispatch before and after it runs”.
Model Async State Explicitly
The simplest useful async shape usually includes at least:
- '
loading,' - the successful data,
- and an error field.
That is better than a single boolean because the UI can distinguish “idle”, “loading”, “failed”, and “loaded”. Good action naming also makes bugs easier to trace later.
Common Pitfalls
- Performing
fetchor other side effects inside the reducer. - Dispatching after unmount because the async request completed late.
- Treating
useReducerdispatch as if thunk middleware were built in. - Collapsing all async outcomes into one flag instead of modeling loading, success, and error separately.
- Forgetting that the reducer’s job is state transition logic, not request orchestration.
Summary
- In React, async work belongs in event handlers, effects, or a separate async layer, not inside reducers.
- Dispatch a start action before the request and success or error actions afterward.
- '
useReducerdispatch expects plain actions by default.' - Guard against stale async completions when the component can unmount.
- Keep reducers pure and let actions describe state changes clearly.
Related reading
- React HOC pattern - dealing with async methods
- React React-router async components mounted multiple times componentDidMount called many times
- React, setState with async updater parameter?
- Read asynchronously data from NetworkStream with huge amount of packets
- React js onClick can't pass value to method
- React Native android build failed. SDK location not found
- Read file in EventMachine asynchronously
- Read Locks and Write Locks
.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.