Middle
Tell me about React hooks: which ones have you used and for what purposes (useState, useEffect, useRef, useContext, useMemo, useCallback)?
sobes.tech AI
Answer from AI
React hooks allow you to use state and other React features in functional components.
-
useState — stores the local state of the component.
const [count, setCount] = useState(0); -
useEffect — performs side effects (requests, subscriptions) after rendering.
useEffect(() => { document.title = `You clicked ${count} times`; }, [count]); -
useRef — stores a mutable object that does not cause re-rendering when changed, often used to access the DOM.
const inputRef = useRef(null); -
useContext — allows using React context to pass data without props.
const theme = useContext(ThemeContext); -
useMemo — memoizes a computed value to avoid unnecessary recalculations.
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]); -
useCallback — memoizes a function to prevent recreating it on every render.
const memoizedCallback = useCallback(() => { doSomething(a, b); }, [a, b]);
I used these hooks for state management, performance optimization, and working with effects and context.