Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

React warns about updating one component while another renders

ErrorWarning: 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.

  • Applies to React 19, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.jsx
  2. 2Match what you see, then jump to its fix

    1. 1The warning fires on every render and a button has onClick={fn(...)} with parentheses.An event handler is called instead of passedFix
    2. 2The 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. 3The 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. 4A 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

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 dev

Confirmed by react.dev

Did this fix it?

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.jsx

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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 dev

Confirmed by react.dev

Did this fix it?

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/ | head

2Fix

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 dev

Confirmed by react.dev

Did this fix it?

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:

Related fixes

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.