Blog/NotesConcept

Understand why are React hooks declarations not allowed inside any function

A quick guide to explain an important react interview question, why React Hooks declarations are not allowed inside functions or any conditional blocks with code example.

Intermediate

Frontendgeek

Last Updated Nov 1, 2025


A Quick Explanation

Because React relies on the order of Hook calls to correctly associate state and effects with their respective components.
If you call Hooks conditionally or inside nested functions, that order can change between renders — and React would lose track of which state belongs to which Hook.

Code Explanation

React uses an internal array (or linked list) to keep track of Hooks for each component.
For example, when your component renders:

function MyComponent() {
  const [count, setCount] = useState(0); // Hook #1
  const [text, setText] = useState('');  // Hook #2
  useEffect(() => console.log(count));    // Hook #3
  ...
}

React doesn’t identify hooks by name — it relies on the order:

  1. First hook → state slot 1
  2. Second hook → state slot 2
  3. Third hook → effect slot 3

Now imagine this:

function MyComponent() {
  const [count, setCount] = useState(0);

  if (count > 0) {
    const [text, setText] = useState('Hello'); // ❌
  }

  useEffect(() => console.log(count));
}

On the first render (count = 0), Hook #2 (the conditional one) doesn’t run.
On the next render (count > 0), it does run — shifting the order of hooks.
React now mismatches the stored hook data (like state and effects), leading to broken or unpredictable behaviour.

Hence, React enforces the Rules of Hooks:

  • ✅ Only call Hooks at the top level of your component.
  • ✅ Only call Hooks from React functions (functional components or custom hooks).

Share this post now:

💬 Comments (0)

Login to comment

Advertisement

Flaunt You Expertise/Knowledge & Help your Peers

Sharing your knowledge will strengthen your expertise on topic. Consider writing a quick Blog/Notes to help frontend folks to ace Frontend Interviews.

Advertisement


Other Related Blogs

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Implement useFetch() Custom Hook in React

Anuj Sharma

Last Updated Oct 28, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Implement Infinite Currying Sum: JavaScript Interview Question sum(1)(2)(3)

Anuj Sharma

Last Updated Oct 26, 2025

In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.

Implement Infinite Currying Multiplication in JavaScript: mul(2)(3)(4)

Anuj Sharma

Last Updated Oct 26, 2025

Understand the step-by-step implementation of Infinite Currying Multiplication in JavaScript with a code example.

Stay Updated

Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2024 FrontendGeek. All rights reserved