Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
React identifies each hook by its call order within a component; if the order changes between renders, state gets attached to the wrong hook.
React stores a component's hook state in a list and matches each call to its slot purely by position. If useState is called inside an if that is true on one render and false on the next, every hook after it shifts by one slot, so values get mixed up or React throws an error. Hooks must therefore be called unconditionally at the top level of a component or custom hook, never in loops, conditions, nested functions or after an early return. Put the condition inside the hook instead, for example inside the effect body.