Cannot update component while rendering - React Fix (2026) | Tutorials Logic
What is "Cannot update a component while rendering a different component"?
This React warning occurs when a component triggers a state update in another component during the render phase. React's rendering must be a pure, side-effect-free process. Calling setState or a state updater function during rendering — even indirectly through a child component — violates this rule and can cause infinite render loops or inconsistent UI states.
Warning: Cannot update a component (App) while rendering a different component
Common Causes
Quick Fix (TL;DR)
// ❌ Problem — setState called during render
function Child({ onUpdate }) {
onUpdate("new value"); // Called during render!
return Child;
}
// ✅ Solution — move to useEffect
function Child({ onUpdate }) {
useEffect(() => {
onUpdate("new value"); // Called after render
}, [onUpdate]);
return Child;
}
Common Scenarios & Solutions
Scenario 1: setState Called in Render Body
Calling a state setter directly in the component body (outside of event handlers or effects) triggers a state update during the render phase, causing React to warn and potentially loop.
function App() {
const [count, setCount] = useState(0);
// ❌ Called during render — causes warning
setCount(count + 1);
return {count};
}
function App() {
const [count, setCount] = useState(0);
// ✅ State updates in event handlers
const handleClick = () => setCount(c => c + 1);
return ;
}
Scenario 2: Child Component Calling Parent setState During Render
When a child component calls a parent's state setter (passed as a prop) directly in its render body, it updates the parent's state while the parent is still rendering, causing this warning.
function Parent() {
const [title, setTitle] = useState("");
return ;
}
function Child({ setTitle }) {
setTitle("Hello from Child"); // ❌ Called during render!
return Child;
}
function Child({ setTitle }) {
// ✅ Use useEffect to update parent after render
useEffect(() => {
setTitle("Hello from Child");
}, []); // Empty deps — runs once after mount
return Child;
}
Scenario 3: useEffect with Missing Dependencies
A useEffect without a dependency array runs after every render. If it updates state, it triggers another render, which runs the effect again — creating an infinite loop that also triggers this warning.
function App() {
const [data, setData] = useState(null);
useEffect(() => {
fetchData().then(result => setData(result));
// ❌ No dependency array — runs after every render!
});
return {data};
}
function App() {
const [data, setData] = useState(null);
useEffect(() => {
fetchData().then(result => setData(result));
}, []); // ✅ Empty array — runs only once after mount
return {data};
}
Scenario 4: Immediately Invoked Event Handler
Passing onClick={setState(value)} instead of onClick={() => setState(value)} immediately calls the function during render instead of waiting for the click event.
function App() {
const [active, setActive] = useState(false);
return (
// ❌ setActive(true) is called immediately during render!
);
}
function App() {
const [active, setActive] = useState(false);
return (
// ✅ Arrow function wraps the call — only runs on click
);
}
Best Practices
Related Errors
- React's render phase must be pure — no state updates, no side effects.
- State updates during render cause this warning and can lead to infinite loops.
-
Move state updates to event handlers or
useEffecthooks. -
Always add a dependency array to
useEffectto control when it runs. -
Pass function references to event handlers:
onClick={fn}notonClick={fn()}. - React Strict Mode double-invokes renders in development to surface these issues early.
Frequently Asked Questions
Level Up Your React js Skills
Master React js with these hand-picked resources