How to use useMemo in React

Optimizing expensive calculations is essential for maintaining smooth user interfaces and preventing performance bottlenecks in React applications. As the creator of CoreUI, a widely used open-source UI library, I’ve optimized countless React components for performance over 25 years of development. From my expertise, the most effective approach is using the useMemo hook to memoize costly computations and only recalculate when dependencies change. This prevents unnecessary work on every render and keeps your app responsive.

Use useMemo to memoize expensive calculations and prevent unnecessary re-computations.

const expensiveValue = useMemo(() => {
  return heavyCalculation(data)
}, [data])

Here useMemo takes a function that performs the expensive calculation and a dependency array [data]. The calculation only runs when data changes, otherwise React returns the cached result from previous renders. This prevents the heavy computation from running on every render, significantly improving component performance when dealing with complex data processing.

Best Practice Note:

This is the same optimization technique we use in CoreUI components for complex data transformations and filtering operations. Only use useMemo for genuinely expensive calculations, as the memoization itself has overhead that may not be worth it for simple operations.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author