useMemo and useCallback: when are they actually worth it?

poze 4 months ago84 gadeen

4

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.

poze 4 months ago

2 Repons

9
✓
✓ Repons yo aksepte

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.

answered 4 months ago
0

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.

answered 4 months ago

Sign in and verify your email to post an answer.