React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)
What happens if you call useState conditionally?
mid LevelCode ExampleHooks
As per rules of React Hooks, hooks must be called unconditionally. For example, if you conditionally call it:
js
if (someCondition) {
const [state, setState] = useState(0);
}
React will throw a runtime error because it relies on the order of Hook calls, and conditional logic breaks that order.