useMemo and useCallback: when are they actually worth it?
asked 4 months ago86 viewsen
A teammate wraps every function in useCallback and every computed value in useMemo. Is that a good practice or premature optimization?
AI diagnosis
AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.
2 Answers
Mostly premature. Memoization has its own cost (memory, comparing dependencies, noisier code). Use it when there's a concrete reason:
1. Expensive computation: e.g. filtering or sorting thousands of rows each render. Use useMemo. 2. Referential stability matters: the value or function is passed to a React.memo child, or used in another hook's dependency array.
const visible = useMemo(() => rows.filter(matches(query)), [rows, query]);
If neither applies, skip it. With the React Compiler (stable in React 19+ setups), components are memoized automatically, and manual useMemo/useCallback become mostly unnecessary.
Sign in to tell others whether this worked.
Measure first. Open the React DevTools Profiler, record the slow interaction, and see which components re-render and how long they take. Optimize the ones that show up there, not everything.
Sign in to tell others whether this worked.
Sign in and verify your email to post an answer.