Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What are React Hooks and why were they introduced? React Hooks क्या हैं और क्यों introduce किए गए?

Answer
// Hooks allow functional components to have state and lifecycle
// Before Hooks: only class components could have state/lifecycle
// After Hooks: functional components became powerful

// useState - add state to functional components
const [count, setCount] = useState(0);

// useEffect - handle side effects
useEffect(() => {
  document.title = `Count: ${count}`;
}, [count]);

// useContext - access context
const theme = useContext(ThemeContext);

// useReducer - complex state management
const [state, dispatch] = useReducer(reducer, initialState);
Hooks का introduced किए गए:
1. Code reusability को easy बनाने के लिए
2. Functional components को powerful बनाने के लिए
3. Complex components को simple बनाने के लिए

Was this answer clear?