Cannot update a component while rendering another component
A child component schedules a state update on its parent during the render phase.
Usually happens because:
- ☑ Child component executes parent setter on render
- ☑ Context updates triggered inside render body
- ☑ Redux dispatch executed directly in function component
🔍 Quick Checklist:
📊 Where this usually happens (estimated occurrence)
What is Cannot update a component while rendering another component?
This warning occurs when a component executes a function that updates the state of another component (usually a parent component via callback props) during its own render phase. React enforces unidirectional data flow and blocks side-effects from running during rendering.
Common Causes
- Calling parent state setters directly inside the body of a rendering child component.
- Triggering context updates inside child component definitions.
Common Mistakes
- Updating Context variables unconditionally inside child component render methods.
How to Fix
Framework-Specific Examples
No framework examples required for this informational status.
Wrong: Parent State set in Render
function Child({ onRender }) {
onRender(); // parent setter called during child rendering
return <div>Child</div>;
}Correct: Parent State set in useEffect
import { useEffect } from 'react';
function Child({ onRender }) {
useEffect(() => {
onRender(); // called safely after mount/render phases complete
}, [onRender]);
return <div>Child</div>;
}Best Practices
- Verify child components only read properties during rendering and trigger modifications inside effects or actions.
Frequently Asked Questions (FAQ)
Q: Why does React block updates during rendering?
Rendering must be a pure function of props and state. Side effects (like scheduling updates on other components) during render break predictability and trigger rendering errors.
Q: How does using useEffect fix this?
useEffect runs after the active render cycle is complete and the layout is painted to the DOM, allowing React to schedule new updates safely.
Q: Can Redux dispatch calls cause this error?
Yes. Dispatching actions inside the render body of a component triggers state changes, raising the same warning.
Q: Does this warning crash my production application?
No. It is a console warning in development, but it indicates poor lifecycle implementation and can lead to unexpected UI glitches.