ReactMedium

Hook performance optimization?

Optimize hooks by avoiding work instead of caching it: derive values during render instead of syncing them with effects, keep state as local as possible, stabilize dependencies, split contexts, use transitions for heavy updates, and memoize only measured hot spots. With the React Compiler, most manual useMemo, useCallback and memo usage can be dropped.

Checklist

  1. Remove unnecessary effects: derived data computed in render avoids an extra render pass per change.
  2. Lazy initial state: useState(() => expensive()) runs the computation once.
  3. Colocate state: fast-changing state (input text, hover) in the smallest component, so fewer components re-render.
  4. Stable dependencies: move objects and functions inside effects, depend on primitives, use updater functions, and useEffectEvent for non-reactive logic.
  5. Context: memoize provider values and split contexts by update frequency. For large global state, use a store with selectors (Zustand, Redux) so components subscribe to slices.
  6. Transitions: useTransition and useDeferredValue keep typing responsive during heavy renders.
  7. Memoization: useMemo for measured expensive computations, useCallback + memo for expensive children.
  8. Avoid creating components inside components (remounts and lost state).
  9. Virtualize long lists.
  10. Debounce or throttle high-frequency events (scroll, resize, typing that triggers fetches).

React Compiler

Version 1.0 memoizes automatically at build time. Enable it via the Vite react-compiler-ts template, babel-plugin-react-compiler, or reactCompiler: true in Next.js, and use the eslint-plugin-react-hooks compiler rules to find code it cannot optimize.

Measure

Use the React DevTools Profiler and Chrome's React Performance tracks before and after changes.

Code example

tsx
1import { useDeferredValue, useMemo, useState } from "react";
2
3type Product = { id: string; name: string; price: number };
4
5export function Catalog({ products }: { products: Product[] }) {
6 const [query, setQuery] = useState("");
7 const [sort, setSort] = useState<"asc" | "desc">("asc");
8 const deferredQuery = useDeferredValue(query); // typing stays responsive
9
10 // Derived in render (memoized because the list is large); no effect + state pair
11 const visible = useMemo(() => {
12 const q = deferredQuery.toLowerCase();
13 const filtered = products.filter((p) => p.name.toLowerCase().includes(q));
14 return filtered.toSorted((a, b) => (sort === "asc" ? a.price - b.price : b.price - a.price));
15 }, [products, deferredQuery, sort]);
16
17 return (
18 <div>
19 <input value={query} onChange={(e) => setQuery(e.target.value)} />
20 <button onClick={() => setSort((s) => (s === "asc" ? "desc" : "asc"))}>Price {sort}</button>
21 <p>{visible.length} products</p>
22 </div>
23 );
24}

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app