React infinite render
The page freezes and the console reports too many renders.
function Profile({ user }) {
const [name, setName] = useState("");
setName(user.name);
return <input value={name} />;
}Reveal investigation and fix ↓Hide solution ↑
- Symptom: rendering calls a state setter, which schedules another render.
- Root cause: synchronization is happening unconditionally in the component body.
- Fix: initialize from props, derive the value, or synchronize in a carefully scoped effect.
- Prevention: treat rendering as pure and avoid duplicated state.
Key idea: Do not update state during render.