Full answer
Normally setting state while rendering is a mistake, and calling another component's setter during render raises an error. There is one supported exception: updating the same component's own state during its render.
How it works. If the component calls its own setter during render, React finishes that render call, throws the result away, and re-runs the component immediately with the updated state, without committing or rendering children in between. The user never sees the intermediate result. Without a guard it loops until React stops with a "Too many re-renders" error, so the call must be conditional.
Use case: reacting to a prop change. The class component API getDerivedStateFromProps did this. In function components you keep the previous prop in state, compare it during render, and update both the stored prop and the dependent state when it differs. The example below does exactly that.
Compare with an effect that resets state: the effect version commits the stale state first, runs the effect, and then renders again, so users can see one frame with outdated selection. The render-phase version is committed once with consistent values.
Better alternatives, in order:
- Compute the value during render if it can be derived (no state needed): store the selected id and derive the object.
- Reset the whole component with a
keywhen an identity changes:<List key={listId} />. - Use the render-time update pattern only when you need to keep some state and adjust part of it.
Rules: only update the same component's state, always guard with a condition comparing values, and keep logic simple. Do not use this pattern for side effects (network, subscriptions).
The example resets a selection when the list changes, and logs how many times the function runs.
import { useState } from 'react';
function Picker({ items }) {
const [prevItems, setPrevItems] = useState(items);
const [selected, setSelected] = useState(null);
if (items !== prevItems) {
console.log('props changed during render: resetting the selection');
setPrevItems(items);
setSelected(null);
}
console.log('Picker body ran, selected=' + selected);
return (
<div>
{items.map((item) => (
<button key={item} aria-pressed={item === selected} onClick={() => setSelected(item)}>
{item}
</button>
))}
</div>
);
}
export default function App() {
const [list, setList] = useState(['Maths', 'Science']);
return (
<div>
<button onClick={() => setList(['Art', 'Music'])}>Change list</button>
<Picker items={list} />
</div>
);
}The check pressed every button in order: Change list first, which changed the items. The log shows one "props changed" line followed by two body lines: the first body run belongs to the discarded pass, and the second to the immediate re-run. Then the other buttons were pressed. Output of the run: Picker body ran, selected=null props changed during render: resetting the selection Picker body ran, selected=null Picker body ran, selected=null Picker body ran, selected=Art Picker body ran, selected=Music
Key points
- A component may update its own state during render if the update is guarded by a condition.
- React discards the render and re-runs the component immediately, before committing or rendering children.
- It replaces getDerivedStateFromProps and avoids the stale frame that an effect causes.
- Prefer deriving values or using a key. Use the pattern only for partial adjustments.
- Never use it for side effects or for other components' state.
Common mistakes
- Calling setState during render without a guard, causing an infinite loop.
- Using this pattern for side effects such as requests.
- Updating a different component's state during render.
- Using an effect to reset state and showing a stale frame.
- Reaching for it when a key would be simpler.
Follow-ups
- Why does an effect-based reset show a stale frame?
- When is a key a better solution?
- What error does React raise for an unguarded update?
Tips
- Mention that it is supported only for the same component.
- Compare with the effect version and its extra frame.
- Offer the key and derived alternatives first.