React introduced Hooks in version 16.8 to simplify component logic and state management without relying on class components. Hooks allow developers to use state, lifecycle methods, and side effects directly inside functional components, making code cleaner, reusable, and easier to test.
Why Use Hooks in React?
Simplify Code – Replace bulky class components with lightweight functions.
Reusability – Encapsulate logic in custom hooks and share across components.
Better Readability – No need for this
or lifecycle method confusion.
Performance – Reduce unnecessary re-renders with hooks like useMemo
and useCallback
.
Migration Friendly – Helps modernize old React apps.
Types of React Hooks
1. Basic Hooks
✅ useState
const [theme, setTheme] = useState("light");
✅ useEffect
useEffect(() => {
fetchUserData();
}, []);
✅ useContext
const theme = useContext(ThemeContext);
2. Additional Hooks
✅ useReducer
const [state, dispatch] = useReducer(cartReducer, initialCart);
✅ useRef
const inputRef = useRef();
useEffect(() => inputRef.current.focus(), []);
✅ useMemo
const filteredData = useMemo(() => data.filter(d => d.includes(query)), [query]);
✅ useCallback
const handleClick = useCallback(() => doSomething(), []);
✅ useLayoutEffect
✅ useImperativeHandle
3. Custom Hooks
function useAuth() {
const [user, setUser] = useState(null);
useEffect(() => {
const loggedInUser = localStorage.getItem("user");
if (loggedInUser) setUser(JSON.parse(loggedInUser));
}, []);
return user;
}
Real-World Scenarios
E-Commerce Website
useReducer
: Manage cart operations (add/remove/update items).
useEffect
: Fetch product list from API.
useContext
: Share user authentication state across app.
Social Media App
useState
: Manage likes/comments count.
useEffect
: Poll notifications from server.
useMemo
: Optimize feed rendering.
Video Streaming Platform
Banking Application
React Hooks Interview Questions and Answers
Q1. Why were hooks introduced in React?
Answer: To simplify state and lifecycle management in functional components, reduce complexity, and improve reusability without converting everything into class components.
Q2. Difference between useState
and useReducer
?
Answer:
useState
: For simple, local state.
useReducer
: For complex state logic with multiple transitions (like Redux-style state).
Q3. What is the difference between useEffect
and useLayoutEffect
?
Answer:
useEffect
: Runs asynchronously after painting the UI.
useLayoutEffect
: Runs synchronously before painting, useful for DOM measurements.
Q4. How do useMemo
and useCallback
differ?
Answer:
Q5. Can hooks be used in class components?
Answer: No, hooks only work in functional components.
Q6. What are rules of hooks?
Answer:
Only call hooks at the top level (not inside loops, conditions, or nested functions).
Only call hooks from React functional components or custom hooks.
Q7. Example of a custom hook?
Answer: useAuth
, useFetch
, useLocalStorage
are popular custom hooks for reusability.
✅ Final Tip : Always explain hooks with real-world use cases (cart, login, API fetch, notifications, etc.), not just definitions. This shows practical understanding.