DEV Community

banti kevat
banti kevat

Posted on • Originally published at itinfohubs.blogspot.com

React Rules of Hooks Explained (Common Mistakes in Hindi)

React Rules of Hooks Explained (Common Mistakes in Hindi)
React Rules of Hooks kya hain aur inhe sahi tareeke se kaise use karein? Is Hindi guide mein common mistakes aur best practices ko step-by-step seekhein.

Doston, kya aapne kabhi socha hai ki jab hum ReactJS mein hooks use karte hain, to React ko kaise pata chalta hai ki kaun sa state kis variable ke liye hai? Jab hum pehli baar hooks seekhte hain, to lagta hai ki ye sirf ek magic hai. Lekin sach ye hai ki React ke andar ek bahut specific mechanism kaam kar raha hota hai jise hum "Rules of Hooks" kehte hain. Agar aapne ye rules miss kar diye, to aapka application random error throw karega ya aapka component sahi se render nahi hoga.

⚡ Quick Answer: React Rules of Hooks do main niyam hain: hooks ko hamesha component ke top level par call karein aur sirf React functions (components ya custom hooks) ke andar use karein. Inhe loops, conditions, ya nested functions ke andar call karna mana hai, kyunki React hooks ki sequence ko yaad rakhne ke liye array ka use karta hai.

React Rules of Hooks kya hain?

React team ne hooks introduce karte waqt do bahut important rules banaye the taaki state aur lifecycle methods ka flow consistent rahe:

  • Only call Hooks at the top level: Hooks ko loops, conditions, ya nested functions ke andar mat likhein.
  • Only call Hooks from React functions: Hooks ko sirf React components ya custom hooks ke andar hi call karein. JavaScript ke normal functions mein inhe call nahi kiya ja sakta.

Hooks internal memory kaise manage karte hain?

Bahut se beginners ko lagta hai ki React hook ke "naam" se state ko track karta hai. Lekin ye galat hai. React hooks ko ek array ki tarah track karta hai. Jab aap component render karte hain, React har hook ko uski position ke hisaab se array mein store kar leta hai.

🏗️ Architecture Diagram: Hook Execution Flow

useState(0)

useEffect()

useContext()

Diagram: React hooks ko index-based sequence mein execute karta hai.

Common Mistakes aur unhe kaise avoid karein

1. Conditional Hooks ka use karna

Sabse badi galti jo naye developers karte hain, wo hai hooks ko if\ condition ke andar daal dena. Aisa karne se hooks ki sequence disturb ho jaati hai.


// GALT TEEKA:
function MyComponent({ loading }) {
  if (loading) return null;
  const [data, setData] = useState(null); // Ye ERROR de sakta hai
  return <div>{data}</div>;
}

Enter fullscreen mode Exit fullscreen mode

Solution: Hooks ko hamesha function ke start mein declare karein, chahe condition kuch bhi ho.

2. Nested Functions mein hooks call karna

Hooks ko kabhi bhi normal event handler ya nested function mein mat likhein. Hooks sirf React component scope mein hi exist karte hain.

Performance aur Best Practices

Jab aap MERN Stack project par kaam kar rahe hon, to performance ka dhyan rakhna zaroori hai. Hamesha eslint-plugin-react-hooks ka use karein. Ye automatic aapke code mein hone wali hooks se related error ko detect kar leta hai aur aapko warning deta hai.

Feature Best Practice Don't Do This
Hook Call Component top level Inside IF/Loops
Source Only React Components Regular JS Functions

Ek aur important baat, official documentation hamesha padhein kyunki hooks ki API waqt ke saath update hoti rehti hai.

Frequently Asked Questions (FAQs)

Q1: Kya main loops ke andar useState call kar sakta hoon?
Nahi, aisa karne se React ka internal index management kharab ho jayega aur application crash ho jayegi. Hooks hamesha top-level par hone chahiye.
Q2: Error kyu aata hai jab hooks conditionally use karte hain?
React hooks ko array of states mein store karta hai. Agar aap condition change hone par hook skip kar denge, to React ka index shift ho jayega aur wo galat state return karega.
Q3: Kya custom hooks mein rules apply hote hain?
Haan, custom hooks bhi hook hain, isliye unhe bhi 'use' prefix ke saath likhna chahiye aur wo rules follow karne chahiye.
Q4: Kaise pata chale ki hook sahi jagah hai?
ESLint ka plugin 'eslint-plugin-react-hooks' use karein, ye automatically aapko bata dega ki aapne rules break kiye hain ya nahi.
Q5: Normal JS function mein hook use kar sakte hain?
Nahi, sirf React functional component ya kisi dusre custom hook ke andar hi hook call kiya ja sakta hai.

Top comments (0)