useMemo memoizes expensive calculations, recomputing only when dependencies change. Here's how to use it effectively.
Basic Usage#
When to Use useMemo#
Dependencies#
useMemo vs useCallback#
Referential Equality#
Conditional Memoization#
Lazy Initialization#
Common Patterns#
When NOT to Use useMemo#
Debugging useMemo#
Best Practices#
Use useMemo When:
✓ Expensive calculations
✓ Referential equality matters
✓ Preventing child re-renders
✓ Deriving complex state
Don't Use When:
✗ Simple calculations
✗ Primitive values
✗ Computation is cheap
✗ Value isn't used in renders
Dependencies:
✓ Include all values used inside
✓ Use specific properties, not objects
✓ Consider dependency stability
✓ Use ESLint exhaustive-deps rule
Performance:
✓ Profile before optimizing
✓ Measure actual impact
✓ Consider component structure
✓ Don't over-optimize
Conclusion#
useMemo optimizes performance by memoizing expensive calculations and maintaining referential equality. Use it for computationally heavy operations, when passing objects/arrays to memoized children, or when deriving complex state. Always include all dependencies and profile your app to ensure memoization actually helps. Remember: premature optimization is the root of all evil - measure first, optimize second.