When React re-renders
Overview
React is fast by default, but as apps grow, you may hit a screen that stutters while typing or a list that takes too long to update. Fixing that starts with understanding exactly when components re-render — and then applying the right tool, in the right order.
The three reasons a component re-renders
- Its own state changed (a
useStatesetter oruseReducerdispatch). - Its parent re-rendered. By default, when a component renders, React renders all of its children too — whether or not their props changed.
- A context it reads changed.
Notice what’s not on the list: “its props changed”. Props changing is a consequence of reason 2, and React re-renders children even when their props are identical.
Re-renders are usually fine
A re-render calls the component function and compares the result — it doesn’t touch the DOM unless something changed. Rendering a typical component takes microseconds. The problem is only:
- a component that does expensive work during render (sorting thousands of rows, heavy calculations, rendering a huge list), or
- a re-render that cascades through a very large tree, many times per second (typing, dragging, animations).
So measure first.
Measuring with the Profiler
In React DevTools → Profiler:
- Click record, perform the slow interaction (type a few characters), stop.
- The flame chart shows every component that rendered and how long it took. Grey components didn’t render.
- In settings, enable “Record why each component rendered” — it tells you whether it was state, props, hooks or the parent.
Also try the React Performance tracks in Chrome DevTools’ Performance panel (React 19.2+), which show React’s scheduling and component work alongside the browser’s own timeline. And always profile a production build (npm run build && npm run preview) for realistic numbers — development mode does extra checks and is much slower.
Fix 1: move state down
Often the cheapest fix is structural. If typing in a field re-renders a big table, ask: does the table’s parent really need to own that state?
// ✗ note lives in Dashboard, so every keystroke re-renders CourseTable
function Dashboard({ courses }) {
const [note, setNote] = useState('');
return (
<>
<textarea value={note} onChange={(e) => setNote(e.target.value)} />
<CourseTable courses={courses} />
</>
);
}
// ✓ the state lives in a small component; CourseTable is unaffected
function NoteBox() {
const [note, setNote] = useState('');
return <textarea value={note} onChange={(e) => setNote(e.target.value)} />;
}
function Dashboard({ courses }) {
return (
<>
<NoteBox />
<CourseTable courses={courses} />
</>
);
}Fix 2: pass JSX as children
When a wrapper must own fast-changing state, let the parent pass the expensive content as children:
function ScrollTracker({ children }) {
const [scrollY, setScrollY] = useState(0);
// …update scrollY on scroll…
return (
<div onScroll={(e) => setScrollY(e.currentTarget.scrollTop)}>
<ProgressBar value={scrollY} />
{children}
</div>
);
}
<ScrollTracker>
<LessonContent /> {/* created by the parent, so it doesn't re-render when scrollY changes */}
</ScrollTracker>children was created by the parent, which didn’t re-render, so React reuses those elements as-is.
Fix 3: memoisation
When restructuring isn’t enough, you can tell React to skip work.
memo
memo wraps a component so it only re-renders when its props have changed (compared with Object.is, prop by prop):
import { memo } from 'react';
const CourseTable = memo(function CourseTable({ courses, onSelect }) {
// …expensive rendering…
});The identity problem
memo only helps if props are the same values between renders. Objects, arrays and functions created during render are new every time:
function Dashboard({ courses }) {
const [note, setNote] = useState('');
return (
<CourseTable
courses={courses}
onSelect={(id) => setSelectedId(id)} // ✗ new function each render → memo is useless
options={{ dense: true }} // ✗ new object each render
/>
);
}useCallback and useMemo
useCallback keeps the same function between renders until its dependencies change; useMemo does the same for a computed value:
import { useCallback, useMemo } from 'react';
function Dashboard({ courses }) {
const [note, setNote] = useState('');
const [selectedId, setSelectedId] = useState(null);
const handleSelect = useCallback((id) => setSelectedId(id), []);
const options = useMemo(() => ({ dense: true }), []);
return <CourseTable courses={courses} onSelect={handleSelect} options={options} />;
}useMemo is also useful on its own for an expensive calculation inside a component:
const sorted = useMemo(
() => courses.toSorted((a, b) => a.title.localeCompare(b.title)),
[courses]
);React recalculates only when courses changes. (Setter functions from useState and dispatch from useReducer are always stable — you can pass them down directly.)
When memoisation is worth it
Memoisation has costs: more code, dependency arrays to keep correct, and memory. It’s worth it when:
- a component is measurably slow to render and re-renders often with the same props,
- a value is passed to a memoised child or used as an effect dependency,
- a calculation takes noticeable time (rough guide: more than a millisecond — wrap it in
console.timeto check).
Sprinkling useMemo and useCallback everywhere “just in case” makes code harder to read for little gain.
React Compiler does this for you
Manually keeping memoisation correct is tedious and error-prone, which is why the React team built React Compiler. It analyses your components at build time and inserts the equivalent of memo, useMemo and useCallback automatically — more precisely than people usually do by hand. With it enabled, most of this lesson’s manual techniques become unnecessary. That’s the next lesson.
Other performance tools
| Problem | Tool |
|---|---|
| Typing feels slow because a big list re-renders | useTransition / useDeferredValue (see Transitions and deferred values) |
| Thousands of rows | list virtualisation (TanStack Virtual, react-window) |
| Large JavaScript bundle | code splitting with lazy (see Code splitting and Activity) |
| Slow network data | caching with TanStack Query, prefetching in loaders |
The order of operations: measure → restructure (move state down, use children) → memoise or enable the compiler → reach for transitions and virtualisation for genuinely heavy UI.
Stop the slow list from re-rendering
In this dashboard, typing in the note field makes a slow CourseTable (it sorts 5,000 rows) re-render on every keystroke. Fix it two ways: first by moving the note state into its own NoteBox component (no memoisation at all); then, in a second version where the note must stay in Dashboard, with memo, useMemo and useCallback. Verify with the React DevTools Profiler or a render counter.
import { useState } from 'react';
function CourseTable({ courses, onSelect }) {
const rows = courses.toSorted((a, b) => a.title.localeCompare(b.title));
return <ul>{rows.map((c) => <li key={c.id} onClick={() => onSelect(c.id)}>{c.title}</li>)}</ul>;
}
export function Dashboard({ courses }) {
const [note, setNote] = useState('');
const [selectedId, setSelectedId] = useState(null);
return (
<>
<textarea value={note} onChange={(e) => setNote(e.target.value)} />
<p>Selected: {selectedId}</p>
<CourseTable courses={courses} onSelect={(id) => setSelectedId(id)} />
</>
);
}import { memo, useCallback, useMemo, useState } from 'react';
// Fix 1: move state down — CourseTable no longer re-renders when the note changes
function NoteBox() {
const [note, setNote] = useState('');
return <textarea value={note} onChange={(e) => setNote(e.target.value)} />;
}
function CourseTableA({ courses, onSelect }) {
const rows = courses.toSorted((a, b) => a.title.localeCompare(b.title));
return <ul>{rows.map((c) => <li key={c.id} onClick={() => onSelect(c.id)}>{c.title}</li>)}</ul>;
}
export function DashboardA({ courses }) {
const [selectedId, setSelectedId] = useState(null);
return (
<>
<NoteBox />
<p>Selected: {selectedId}</p>
<CourseTableA courses={courses} onSelect={setSelectedId} />
</>
);
}
// Fix 2: memoise when the state has to stay in the parent
const CourseTableB = memo(function CourseTableB({ courses, onSelect }) {
const rows = useMemo(
() => courses.toSorted((a, b) => a.title.localeCompare(b.title)),
[courses]
);
return <ul>{rows.map((c) => <li key={c.id} onClick={() => onSelect(c.id)}>{c.title}</li>)}</ul>;
});
export function DashboardB({ courses }) {
const [note, setNote] = useState('');
const [selectedId, setSelectedId] = useState(null);
const handleSelect = useCallback((id) => setSelectedId(id), []);
return (
<>
<textarea value={note} onChange={(e) => setNote(e.target.value)} />
<p>Selected: {selectedId}</p>
<CourseTableB courses={courses} onSelect={handleSelect} />
</>
);
}Summary
- A component re-renders when its state changes, its parent re-renders, or a context it reads changes.
- Re-rendering is usually cheap; measure with the React DevTools Profiler before optimising.
- Moving state down and passing JSX as children often removes unnecessary renders with no memoisation.
memoskips re-rendering when props are equal; it only works if object and function props keep their identity.useMemocaches a calculated value anduseCallbackcaches a function between renders — or let React Compiler do it.