Skip to content
HooksChapter 15 / 37Beginner4 min read

Rules of Hooks

React rules of hooks kya hain, hooks sirf top level aur React functions mein kyun call hote hain, aur ESLint plugin kaise madad karta hai, Roman Urdu mein.

On this page

1. Rules of Hooks kya hain?

Hooks woh functions hain jin ka naam use se shuru hota hai: useState, useEffect, useRef waghera. Yeh React ki taqat use karne ka tareeqa hain, lekin inke saath kuch pabandiyan hain.

2. Kyun zaroori hai?

React hooks ko naam se nahi, order se pehchanta hai. Har render par hooks bilkul usi tarteeb se call hone chahiye.

Rules tornaRules follow karna
if ke andar hook, kabhi chale kabhi nahiHar render par same hooks same order
React state galat hook ko de deta haiHar hook ko sahi state milti hai
Ajeeb bugs aur crashPredictable behaviour
Debug karna bohot mushkilLinter pehle hi pakar leta hai

3. Kahan use hota hai?

Yeh rules har jagah apply hote hain jahan hooks hain:

JagahAllowed?
Function component ki top levelHaan
Custom hook (naam use se shuru) ki top levelHaan
if, for, while ke andarNahi
Early return ke baadNahi
Event handler ya useEffect callback ke andarNahi
Normal JavaScript function meinNahi

4. Kaise kaam karta hai?

React har component ke liye hooks ki ek list rakhta hai. Pehla useState call slot 1, doosra slot 2, aur aise hi.

Hooks ka order
Render 1: useState name, useState age, useEffect
React slots save karta hai: 1 name, 2 age, 3 effect
Render 2: same order mein same hooks
Slot 1 name ko, slot 2 age ko milta hai

Agar render 2 mein pehla hook kisi if ki wajah se skip ho jaye, to age wala hook slot 1 padh lega aur sab kuch ek jagah khisak jayega.

5. Code example

Galat: condition ke andar hook

jsx
function Profile({ isLoggedIn }) {
  if (isLoggedIn) {
    const [name, setName] = useState("");
  }
  const [theme, setTheme] = useState("light");
}

Sahi: hook top par, condition andar

jsx
function Profile({ isLoggedIn }) {
  const [name, setName] = useState("");
  const [theme, setTheme] = useState("light");

  if (!isLoggedIn) {
    return <p>Pehle login karein</p>;
  }

  return <input value={name} onChange={(e) => setName(e.target.value)} />;
}

Linter setup

bash
npm install -D eslint-plugin-react-hooks

Code kaise kaam kar raha hai?

CheezMatlab
Pehla exampleisLoggedIn badla to hooks ki ginti badal gayi, React confuse
Doosra exampleDono hooks hamesha chalte hain, sirf JSX conditional hai
Early returnHooks ke baad hai, isliye safe
eslint-plugin-react-hooksRules tootne par editor mein error dikhata hai

Vite aur Next.js ke templates mein yeh plugin aam taur par pehle se laga hota hai.

6. Common mistakes

Galati: Early return ke baad hook likhna

Why? Kuch renders mein return pehle ho jata hai aur neeche wala hook skip, order toot jata hai.

Behtar: Saare hooks component ke bilkul upar likho, return aur conditions unke baad.

Galati: Event handler ke andar useState ya useEffect call karna

Why? Handler render ke dauran nahi chalta, hook ka slot bana hi nahi sakta aur React error deta hai.

Behtar: Hook top level par banao, handler mein sirf setter call karo.

Galati: Custom function mein hook use karna jis ka naam use se shuru nahi hota

Why? React aur linter usay normal function samajhte hain aur rules check nahi hote.

Behtar: Hooks wale function ka naam use se shuru karo, jaise useWindowSize.

Interview tip

"Hooks loop mein kyun nahi?" Jawab: React hooks ko call order se track karta hai. Loop ya condition order badal sakti hai, isliye hooks top level par hone chahiye.

7. Pehle aur baad mein kya seekhein?

Final quick revision

PointDetail
Rule 1Sirf top level par, conditions aur loops se bahar
Rule 2Sirf components aur custom hooks mein
WajahReact hooks ko order se pehchanta hai
Madadgareslint-plugin-react-hooks
Exceptionuse() conditionally call ho sakti hai