MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Rules of React Hooks0%
Study Materials
React.js for BeginnersProgress 0%
1.1Why React and Single-Page Architecture1.2Setting Up Modern React with Vite1.3React Project Directory Structure and Entry Points1.4Virtual DOM and React Render Cycle Explained
8.1Rules of React Hooks8.2The useEffect Hook for Side Effects8.3The Dependency Array and Execution Lifecycle8.4Cleanup Functions in useEffect
HomeTutorialsReact.js for BeginnersRules of React Hooks
The useEffect Hook for Side Effects

Rules of React Hooks

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. What Are React Hooks?

Introduced in React 16.8, Hooks are functions that let you "hook into" React state and lifecycle features from functional components. Examples include useState, useEffect, useContext, useRef, and useMemo.

Because React relies on the internal execution order of functions to track hook state behind the scenes, you must adhere strictly to The Two Rules of Hooks.

1React's Internal Fiber Node:
2[Hook 1: useState(0)] ──> [Hook 2: useEffect(...)] ──> [Hook 3: useState('')]
3(Hooks are stored as a singly linked list; order MUST NEVER CHANGE between renders!)

2. Rule 1: Only Call Hooks at the Top Level

Do NOT call Hooks inside loops, conditions, nested functions, or try/catch blocks.

Always call Hooks at the very top level of your React function, before any early returns:

1// ❌ FORBIDDEN: Calling hook inside a condition!
2function UserProfile({ userId }) {
3 if (userId) {
4 const [user, setUser] = useState(null); // Violates Rule 1!
5 }
6 // ...
7}
8 
9// ❌ FORBIDDEN: Calling hook inside a loop!
10function FeatureMatrix({ features }) {
11 for (let i = 0; i < features.length; i++) {
12 useEffect(() => {}); // Violates Rule 1!
13 }
14 // ...
15}
16 
17// ✅ CORRECT: Always call at the top level
18function UserProfile({ userId }) {
19 const [user, setUser] = useState(null);
20 
21 useEffect(() => {
22 // Put conditions INSIDE the hook, not around it!
23 if (userId) {
24 // Fetch user data...
25 }
26 }, [userId]);
27 
28 if (!userId) return <p>Please select a user.</p>;
29 
30 return <div>{user?.name}</div>;
31}

Why does this rule exist?

React does not track hooks by name or ID. Instead, React maintains an internal array / linked list of hook cells for each component instance. On every render, React matches each hook call with the next cell in the list. If a hook is skipped inside an if block, every subsequent hook shifts out of position, permanently corrupting the component's state!

3. Rule 2: Only Call Hooks from React Functions

Do NOT call Hooks from regular JavaScript functions.

You must only call Hooks from:

  1. 1
    React Functional Components: Functions that return JSX and start with an uppercase letter.
  2. 2
    Custom Hooks: Functions whose names start with use (e.g., useFetch, useAuth, useLocalStorage).
1// ❌ FORBIDDEN: Calling hook from a plain helper function
2function calculateDiscount(price) {
3 const [discount, setDiscount] = useState(0.1); // Violates Rule 2!
4 return price * (1 - discount);
5}
6 
7// ✅ CORRECT: Calling hook inside a Custom Hook
8function useDiscount(price) {
9 const [discount, setDiscount] = useState(0.1);
10 return price * (1 - discount);
11}

4. The ESLint Plugin for Hooks

React provides an official ESLint plugin: eslint-plugin-react-hooks.

It includes two essential linting rules:

  • react-hooks/rules-of-hooks: Enforces the two core rules automatically in your IDE and build step.
  • react-hooks/exhaustive-deps: Verifies that all reactive dependencies are included in useEffect and useCallback dependency arrays.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why must React Hooks only be called at the top level of a component?
Question 2
From which locations are you allowed to invoke a React Hook?
Question 3
What should you do if you only want an effect to run when a certain condition is met?
Question 4
What prefix must all custom React hooks begin with to adhere to the Rules of Hooks?
Question 5
What happens if a component places a useState hook after an early if (error) return null;?
Next Lesson

The useEffect Hook for Side Effects

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Managing Loading, Error, and Empty StatesThe useEffect Hook for Side Effects

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext