Skip to content

Warning: Cannot update a component while rendering a different component

Error message

                Cannot update a component (`X`) while rendering a different component (`Y`)
              

Quick summary

A component called setState on a different component during the render phase, which React does not allow.

Why this happens

  • A child component calls a parent's setState function during render

  • A component updates a context value during another component's render

  • Calling a prop callback that triggers state update during render

Minimal example

✗ Broken code
function Child({ onUpdate }) {
  onUpdate(); // Called during render!
  return <div />;
}
✓ Fixed code
function Child({ onUpdate }) {
  useEffect(() => {
    onUpdate();
  }, [onUpdate]);
  return <div />;
}

Moving the onUpdate call into useEffect ensures it runs after render, not during.

How to diagnose

  • Look for setState calls during render (not in event handlers or effects)

  • Check if a child is calling a parent callback during its render

  • Move the state update to a useEffect

How to fix

  • Move the setState call into a useEffect

  • Move the state update to an event handler

  • Lift the state up so only the owner updates it

How to prevent

  • Never call setState during render — only in event handlers or effects

  • Use useEffect for state updates that depend on other components

Related Resources

Related Glossary

Related Lessons

  • State

    Learn when to update state.

← Back to language