Contents of React Interview Questions

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

What is the difference between React.memo and useMemo?

Though React.memo and useMemo both help optimize performance in React apps by avoiding unnecessary computations or re-renders, they are used in different situations:

AspectReact.memouseMemo
What it optimizesComponent re-rendersExpensive calculations or derived values
Usage targetEntire functional componentsInside functional components
How it worksMemorizes the component output (render)Memorizes the return value of a function
Syntax exampleReact.memo(Component)const memoizedValue = useMemo(fn, deps)

Example of React.memo:

const MyButton = React.memo(function MyButton({ label }) { return <button>{label}</button>; });

The MyButton component won't re-render if the label prop doesn't change.

Example of useMemo:

const sortedItems = useMemo(() => { return items.sort((a, b) => a.value - b.value); }, [items]);

Here, sorting only happens again when items changes.

Summary:

  • Use React.memo when you want to optimize entire components.
  • Use useMemo when you want to optimize expensive computations inside components.