React warns when one component sets another component's state during render. Learn why this breaks rendering rules and how to move the update into an event, an effect or derived data.
The short answer
#A child component is calling a state setter that belongs to a parent, or to a shared store, while the child is rendering. React only allows a component to update its own state during render. Move the update into an event handler or an effect, or remove the state and derive the value instead.
_10function Child({ items, onCount }: Props) {_10- onCount(items.length); // runs during render_10+ useEffect(() => {_10+ onCount(items.length);_10+ }, [items.length, onCount]);_10 return <List items={items} />;_10 }
Why it happens
#Rendering is supposed to be a pure calculation: given props and state, return some JSX. While React is rendering component B, it is in the middle of working out what the screen should look like. If B changes the state of component A at that moment, A's earlier output is now out of date and React has to go back and render A again, possibly in a loop.
React handles this, but it logs the warning because the pattern is fragile and often hides a design problem. The full message names both components, for example "Cannot update a component (App) while rendering a different component (Child)", and points at the line in the stack trace.
Typical causes are:
- Calling a callback prop such as
onChangeoronCountdirectly in the body of a child. - Dispatching to a global store (Redux, Zustand, a context setter) during render.
- Calling a parent's setter inside a
.mapor a helper function that runs while rendering. - A form library or router hook that updates shared state during render because of how it is called.
How to fix it
#Derive the value in the parent
Often the parent does not need the child to report anything. If the parent already has the data, compute what it needs from it directly.
_11function App() {_11 const items = useItems();_11 const count = items.length; // no state, no callback_11_11 return (_11 <>_11 <Header count={count} />_11 <Child items={items} />_11 </>_11 );_11}
This is the cleanest fix because there is nothing to keep in sync.
Update in response to an event
If the parent should change when the user does something in the child, call the callback from the event handler, not from the render body.
_10function SearchBox({ onSearch }: { onSearch: (query: string) => void }) {_10 return <input onChange={(event) => onSearch(event.target.value)} />;_10}
Move it into an effect
When the update really must follow a render, such as reporting a measured value or syncing with an external store, put it in useEffect. The effect runs after React has committed the render, so the update starts a fresh render instead of interrupting one. Keep the dependency list accurate, and wrap callback props in useCallback in the parent if they would otherwise change every render.
Lift the state up
If two components both need to read and change the same value, move the state to their closest common parent and pass it down. Neither child then has to push changes upward during render.
Common pitfalls
#- Fixing the warning with
setTimeout. It defers the update but leaves the extra render, and makes the order of updates harder to follow. - Moving the update into an effect without a dependency array. The effect then runs after every render and can cause a loop.
- Treating it as harmless because the screen looks right. The extra renders cost time and can produce flicker or inconsistent UI.
- Looking only at the component named first. The second name in the message is the one doing the update.
Related reading
#Was this resource helpful?