useMemo and useCallback: when are they actually worth it?
poze 4 months ago84 gadeen
A teammate wraps every function in useCallback and every computed value in useMemo. Is that a good practice or premature optimization?
Dyagnostik IA
Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.
2 Repons
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.
Konekte pou di lòt moun si li te mache.
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.
Konekte pou di lòt moun si li te mache.
Sign in and verify your email to post an answer.