What is the useCallback hook?
Why Interviewers Ask This
This is a classic screening question for React.js roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.
Answer
useCallback returns a memoized version of a callback function — the same function reference is returned on subsequent renders unless the dependencies change. Syntax: const memoizedFn = useCallback(() => doSomething(a, b), [a, b]);. Why it matters: in JavaScript, every function defined inside a component is a new function reference on every render. When you pass a callback to a child component, the child receives a new reference each time, causing it to re-render even if nothing meaningful changed. With useCallback, the function reference stays stable as long as dependencies do not change — preventing unnecessary child re-renders when combined with React.memo. Key relationship: useCallback(fn, deps) is equivalent to useMemo(() => fn, deps). When to use: (1) Passing callbacks to memoized child components (React.memo). (2) Dependencies in useEffect that should not re-run just because a function was recreated. (3) Event handlers passed to frequently re-rendering children. Overusing useCallback is a performance anti-pattern — it adds overhead and memory usage.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last React.js project, I used this when...' immediately makes your answer more credible and memorable.