Skip to main content
InterviewHubPRO
Section Overview
Q268

Question 268 of 407

Part 9: React Hooks Deep Dive (262 - 316)

66%
React Core & Hooks (407 Qs)•Part 9: React Hooks Deep Dive (262 - 316)

Can you describe the useMemo() Hook?

mid LevelCode ExampleHooks

The useMemo() Hook in React is used to optimize performance by caching the result of an expensive calculation between re-renders. It takes a calculate function and a list of dependencies, re-computing the value only when one of the dependencies has changed:

jsx
import React, { useMemo } from 'react';

function ProductList({ products, filterQuery }) {
  // Re-computes filtered list only when products or filterQuery change
  const filteredProducts = useMemo(() => {
    return products.filter(p => p.name.toLowerCase().includes(filterQuery.toLowerCase()));
  }, [products, filterQuery]);

  return <ul>{filteredProducts.map(p => <li key={p.id}>{p.name}</li>)}</ul>;
}