How to optimize React renders with useMemo and useCallback
· Category: React
Short answer
Wrap expensive calculations in useMemo and pass stable function references with useCallback to child components that rely on reference equality. For broader performance tuning, see how to improve core web vitals. For component architecture, see what are react server components.
Steps
- Identify expensive computations inside components
- Wrap with
useMemo(() => compute(data), [data]) - Wrap callbacks passed to optimized children with
useCallback - Ensure dependency arrays are complete and correct
- Profile with React DevTools Profiler before and after
Tips
- Do not memoize everything; only optimize measured bottlenecks
useCallbackis most useful when passed to memoized child components- For authentication flows that trigger re-renders, see how to implement authentication in react