React warns about updating one component while another renders
Warning: Cannot update a component (`Parent`) while rendering a different component (`Child`)Short answer
React renders must be pure. This warning means a component called another component's state setter, or a parent callback, while it was rendering. Find the call with the stack in the warning, then move it into an event handler or, if nothing else fits, an effect.
Fix it fast
1Find out which cause you have
Read the two component names in the warning. The second one (the one rendering) contains the bad call. Open it and search for any function that came from props or a store, and is called outside a handler.
grep -n "set[A-Z][A-Za-z]*(\|props\.on[A-Z]" src/components/Child.jsx2Match what you see, then jump to its fix
- 1
The warning fires on every render and a button has onClick={fn(...)} with parentheses.An event handler is called instead of passedFix - 2
The call sits directly in the function body, for example if (!user) props.setError('...') before the return.The render body calls a state setter that belongs to another componentFix - 3
The warning appears right after a setX(prev => ...) call that also contains another set call or a parent callback.A state updater function triggers another updateFix - 4
A child copies props or fetched data into the parent's state while rendering.Data that could be computed is mirrored into a parent with setStateFix
- 1
What does the error look like?
Warning: Cannot update a component (`Parent`) while rendering a different component (`Child`)To locate the bad setState() call inside `Child`, follow the stack trace
Cause 1 of 4
An event handler is called instead of passed
1Check
In JSX, braces run immediately. onClick={setOpen(true)} runs during render, and its return value becomes the prop.
grep -rn "onClick={[a-zA-Z.]*(" src/2Fix
Pass a function, or wrap the call in an arrow function so it only runs on click.
// wrong
<button onClick={props.onSelect(item.id)}>Pick</button>
// right
<button onClick={() => props.onSelect(item.id)}>Pick</button>3Verify
Reload the page. The warning should be gone and the click should still work.
npm run devConfirmed by react.dev
Cause 2 of 4
The render body calls a state setter that belongs to another component
1Check
React lets a component set its own state while rendering in one narrow pattern, but calling another component's setter is an error. Render must only return JSX.
grep -n "props.set\|onChange(\|onLoaded(" src/components/Child.jsx2Fix
Run the update in the handler that caused the change. If the child must report something after it renders, do it in an effect.
useEffect(() => {
if (!user) props.setError('Not signed in');
}, [user, props.setError]);3Verify
Render the page again and watch the console for the warning.
npm run devConfirmed by react.dev
Cause 3 of 4
A state updater function triggers another update
1Check
React runs updater functions during the next render, and in Strict Mode it runs them twice. They must be pure and only return the next state.
grep -n "=> {" src/components/Child.jsx | grep "set[A-Z]"2Fix
Compute the next value in the handler, set it once, and call the other setter or callback next to it, not inside the updater.
function handleAdd() {
const next = [...items, newItem];
setItems(next);
props.onCountChange(next.length);
}3Verify
Click the control and check that the warning no longer prints.
npm run devConfirmed by react.dev
Cause 4 of 4
Data that could be computed is mirrored into a parent with setState
1Check
The React docs name moving the update into the event handler and removing redundant state as the first options.
grep -rn "useState" src/ | head2Fix
Derive the value where it is needed, or call the parent's callback from the handler that changes the data.
const total = items.reduce((sum, i) => sum + i.price, 0);3Verify
The parent shows the value without any setter call during render.
npm run devConfirmed by react.dev
How do I stop it coming back?
- Keep component bodies free of any call that changes state, a store or the DOM.
- Leave React Strict Mode on in development: it runs components twice and exposes these calls.
- Pass event handlers by reference, or wrap them in an arrow function.
- Reach for an effect only when no event handler fits.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixWeb apps
Warning: Extra attributes from the server in the console
Text content does not match server-rendered HTML
React found attributes on an element in the server HTML that its own render did not produce.
3 causes, each with a fixWeb apps
Next.js stops with 'document is not defined' while it renders on the server
ReferenceError: document is not defined
Next.js renders your components on the server first, where there is no document.
3 causes, each with a fixWeb apps
A custom error from the server arrives at the client as a generic message
error.digest holds a hash instead of your message
Next.js hides the message of errors thrown in Server Components in production, to avoid leaking details.
3 causes, each with a fixWeb apps
useState fails in a child component in the Next.js App Router
React client hook in Server Component
In the App Router every component is a Server Component unless a file above it is marked otherwise, and Server Components cannot use state or effects.
3 causes, each with a fixWeb apps
React warns that a div is nested inside a p tag
Text content does not match server-rendered HTML
A p element may contain only phrasing content, and the HTML parser closes a paragraph as soon as a div, list or other block element starts.
3 causes, each with a fix