Interview question
What are the rules of Hooks? Hooks के rules क्या हैं?
Answer
// Rules of Hooks (must follow)
// 1. Only call hooks at the top level
// DON'T - call inside conditions
function BadComponent({ condition }) {
if (condition) {
const [state, setState] = useState(0); // WRONG!
}
}
// DO - call at top level
function GoodComponent({ condition }) {
const [state, setState] = useState(0); // CORRECT
}
// 2. Only call hooks from React functions
// DON'T - call from regular JavaScript functions
function myFunction() {
useState(0); // WRONG!
}
// DO - call from components or custom hooks
function MyComponent() {
useState(0); // CORRECT
}
// 3. Use ESLint plugin
import { useEffect, useState } from 'react';
// Dependency array is critical
useEffect(() => {
console.log('Effect');
}, [dependency]); // Don't forget dependency array!Hooks के rules:
1. Only call at top level (না conditions में)
2. Only from React functions या custom hooks
3. ESLint plugin use करो
4. Dependency array को सही set करोWas this answer clear?