InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
What are the different Hooks you have used in ReactJS?
1: useState
Used to create state variables in functional components.
const [count, setCount] = useState(0);
2: useEffect
Used to run side effects (like fetching data or updating the DOM).
useEffect(() => {
console.log('Component mounted');
}, []);
3: useContext
Used to access values from React Context.
const user = useContext(UserContext);
4: useRef
Used to create a reference to a DOM element or a value that persists across renders.
const inputRef = useRef(null);
5: useMemo
Used to memoize expensive calculations.
const result = useMemo(() => expensiveCalculation(a, b), [a, b]);
6: useCallback
Used to memoize functions so they aren’t recreated every render.
const handleClick = useCallback(() => {
alert('Clicked!');
}, []);
7: useReducer
Used for more complex state logic (like Redux, but simpler).
const [state, dispatch] = useReducer(reducer, initialState);
Tip:
1: Hooks always start with “use”.
2: They help manage state, side effects, and other React features in functional components.
You can remember the main hooks as: useState, useEffect, useContext, useRef, useMemo, useCallback, useReducer