Skip to main content
InterviewHubPRO
Section Overview
Q273

Question 273 of 407

Part 9: React Hooks Deep Dive (262 - 316)

67%
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.