Contents of React Interview Questions

Comprehensive collection of React interview questions and answers covering hooks, components, state management, and best practices.

What is useCallback and when should it be used?

useCallback is a Hook that returns a memoized version of a function, which only changes if one of its dependencies changes. It helps to prevent unnecessary re-creations of functions between renders, which is useful when passing functions to child components that rely on reference equality (like React.memo).

Syntax:

const memoizedCallback = useCallback(() => { doSomething(a, b); }, [a, b]);

When to use useCallback:

  • When passing functions as props to memoized components.
  • When a function is used inside a useEffect dependency array.
  • When you want to optimize performance by avoiding re-renders due to new function instances.

⚡ Quick Tip: Just like useMemo, don't use useCallback everywhere. It's mainly useful in performance-critical paths.