React Component Performance
Overview
Identify render hotspots, isolate expensive updates, and apply targeted optimizations without changing UI behavior.
When to Use
- When the user asks to profile or improve a slow React component.
- When you need to reduce re-renders, list lag, or expensive render work in React UI.
Workflow
- Reproduce or describe the slowdown.
- Identify what triggers re-renders (state updates, props churn, effects).
- Isolate fast-changing state from heavy subtrees.
- Stabilize props and handlers; memoize where it pays off.
- Reduce expensive work (computation, DOM size, list length).
- Validate: open React DevTools Profiler → record the interaction → inspect the Flamegraph for components rendering longer than ~16 ms → compare against a pre-optimization baseline recording.
Checklist
- Measure: use React DevTools Profiler or log renders; capture baseline.
- Find churn: identify state updated on a timer, scroll, input, or animation.
- Split: move ticking state into a child; keep heavy lists static.
- Memoize: wrap leaf rows with
memo only when props are stable.
- Stabilize props: use
useCallback/useMemo for handlers and derived values.
- Avoid derived work in render: precompute, or compute inside memoized helpers.
- Control list size: window/virtualize long lists; avoid rendering hidden items.
- Keys: ensure stable keys; avoid index when order can change.
- Effects: verify dependency arrays; avoid effects that re-run on every render.
- Style/layout: watch for expensive layout thrash or large Markdown/diff renders.
Optimization Patterns
Isolate ticking state
Move a timer or animation counter into a child so the parent list never re-renders on each tick.
// ❌ Before – entire parent (and list) re-renders every second
function Dashboard({ items }: { items: Item[] }) {
const [tick, setTick] = useState(0);
useEffect(() => {
const id = setInterval(() => setTick(t => t + 1), 1000);
return () => clearInterval(id);
}, []);
return (
<>
<Clock tick={tick} />
<ExpensiveList items={items} /> {/* re-renders every second */}
</>
);
}
// ✅ After – only <Clock> re-renders; list is untouched
function Clock() {
const [tick, setTick] = useState(0);
useEffect(() => {
const id = setInterval(() => setTick(t => t + 1), 1000);
return () => clearInterval(id);
}, []);
return <span>{tick}s</span>;
}
function Dashboard({ items }: { items: Item[] }) {
return (
<>
<Clock />
<ExpensiveList items={items} />
</>
);
}
Stabilize callbacks with useCallback + memo
// ❌ Before – new handler reference on every render busts Row memo
function List({ items }: { items: Item[] }) {
const handleClick = (id: string) => console.log(id); // new ref each render
return items.map(item => <Row key={item.id} item={item} />);
}
// ✅ After – stable handler; Row only re-renders when its own item changes
const Row = memo(({ item, onClick }: RowProps) => (
<li => onClick(item.id)}>{item.name}</li>
));
function List({ items }: { items: Item[] }) {
const handleClick = useCallback((id: string) => console.log(id), []);
return items.map(item => <Row key={item.id} item={item} />);
}
Prefer derived data outside render
// ❌ Before – recomputes on every render
function Summary({ orders }: { orders: Order[] }) {
const total = orders.reduce((sum, o) => sum + o.amount, 0); // runs every render
return <p>Total: {total}</p>;
}
// ✅ After – recomputes only when orders changes
function Summary({ orders }: { orders: Order[] }) {
const total = useMemo(() => orders.reduce((sum, o) => sum + o.amount, 0), [orders]);
return <p>Total: {total}</p>;
}
Additional patterns
- Split rows: extract list rows into memoized components with narrow props.
- Defer heavy rendering: lazy-render or collapse expensive content until expanded.
Profiling Validation Steps
- Open React DevTools → Profiler tab.
- Click Record, perform the slow interaction, then Stop.
- Switch to Flamegraph view; any bar labeled with a component and time > ~16 ms is a candidate.
- Use Ranked chart to sort by self render time and target the top offenders.
- Apply one optimization at a time, re-record, and compare render counts and durations against the baseline.
Example Reference
Load references/examples.md when the user wants a concrete refactor example.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for enprojectnment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
1---2name: react-component-performance3description: Diagnose slow React components and suggest targeted performance fixes.4---56# React Component Performance78## Overview910Identify render hotspots, isolate expensive updates, and apply targeted optimizations without changing UI behavior.1112## When to Use13- When the user asks to profile or improve a slow React component.14- When you need to reduce re-renders, list lag, or expensive render work in React UI.1516## Workflow17181. Reproduce or describe the slowdown.192. Identify what triggers re-renders (state updates, props churn, effects).203. Isolate fast-changing state from heavy subtrees.214. Stabilize props and handlers; memoize where it pays off.225. Reduce expensive work (computation, DOM size, list length).236. **Validate**: open React DevTools Profiler → record the interaction → inspect the Flamegraph for components rendering longer than ~16 ms → compare against a pre-optimization baseline recording.2425## Checklist2627- Measure: use React DevTools Profiler or log renders; capture baseline.28- Find churn: identify state updated on a timer, scroll, input, or animation.29- Split: move ticking state into a child; keep heavy lists static.30- Memoize: wrap leaf rows with `memo` only when props are stable.31- Stabilize props: use `useCallback`/`useMemo` for handlers and derived values.32- Avoid derived work in render: precompute, or compute inside memoized helpers.33- Control list size: window/virtualize long lists; avoid rendering hidden items.34- Keys: ensure stable keys; avoid index when order can change.35- Effects: verify dependency arrays; avoid effects that re-run on every render.36- Style/layout: watch for expensive layout thrash or large Markdown/diff renders.3738## Optimization Patterns3940### Isolate ticking state4142Move a timer or animation counter into a child so the parent list never re-renders on each tick.4344```tsx45// ❌ Before – entire parent (and list) re-renders every second46function Dashboard({ items }: { items: Item[] }) {47 const [tick, setTick] = useState(0);48 useEffect(() => {49 const id = setInterval(() => setTick(t => t + 1), 1000);50 return () => clearInterval(id);51 }, []);52 return (53 <>54 <Clock tick={tick} />55 <ExpensiveList items={items} /> {/* re-renders every second */}56 </>57 );58}5960// ✅ After – only <Clock> re-renders; list is untouched61function Clock() {62 const [tick, setTick] = useState(0);63 useEffect(() => {64 const id = setInterval(() => setTick(t => t + 1), 1000);65 return () => clearInterval(id);66 }, []);67 return <span>{tick}s</span>;68}6970function Dashboard({ items }: { items: Item[] }) {71 return (72 <>73 <Clock />74 <ExpensiveList items={items} />75 </>76 );77}78```7980### Stabilize callbacks with `useCallback` + `memo`8182```tsx83// ❌ Before – new handler reference on every render busts Row memo84function List({ items }: { items: Item[] }) {85 const handleClick = (id: string) => console.log(id); // new ref each render86 return items.map(item => <Row key={item.id} item={item} onClick={handleClick} />);87}8889// ✅ After – stable handler; Row only re-renders when its own item changes90const Row = memo(({ item, onClick }: RowProps) => (91 <li onClick={() => onClick(item.id)}>{item.name}</li>92));9394function List({ items }: { items: Item[] }) {95 const handleClick = useCallback((id: string) => console.log(id), []);96 return items.map(item => <Row key={item.id} item={item} onClick={handleClick} />);97}98```99100### Prefer derived data outside render101102```tsx103// ❌ Before – recomputes on every render104function Summary({ orders }: { orders: Order[] }) {105 const total = orders.reduce((sum, o) => sum + o.amount, 0); // runs every render106 return <p>Total: {total}</p>;107}108109// ✅ After – recomputes only when orders changes110function Summary({ orders }: { orders: Order[] }) {111 const total = useMemo(() => orders.reduce((sum, o) => sum + o.amount, 0), [orders]);112 return <p>Total: {total}</p>;113}114```115116### Additional patterns117118- **Split rows**: extract list rows into memoized components with narrow props.119- **Defer heavy rendering**: lazy-render or collapse expensive content until expanded.120121## Profiling Validation Steps1221231. Open **React DevTools → Profiler** tab.1242. Click **Record**, perform the slow interaction, then **Stop**.1253. Switch to **Flamegraph** view; any bar labeled with a component and time > ~16 ms is a candidate.1264. Use **Ranked chart** to sort by self render time and target the top offenders.1275. Apply one optimization at a time, re-record, and compare render counts and durations against the baseline.128129## Example Reference130131Load `references/examples.md` when the user wants a concrete refactor example.132133## Limitations134- Use this skill only when the task clearly matches the scope described above.135- Do not treat the output as a substitute for enprojectnment-specific validation, testing, or expert review.136- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.