Sub topic 1
Why components re-render and why it sometimes matters
Prerequisites
This chapter assumes you already know:
- How to write function components that take props and hold state with
useState. - What
useMemoanduseCallbackdo, at least in outline. - How to read JSX and basic JavaScript (arrow functions, object identity).
You don't need any build tool knowledge yet. Later chapters cover setup.
How a re-render starts
A render is React calling your component function to get a description of the UI. A re-render is the same call made again after something changed. React re-renders a component when:
- Its own state changes (a
setStatecall with a new value). - Its parent re-renders. This is the cause people forget most often.
- A context it reads changes.
The second rule matters most. When a parent re-renders, React re-renders all of its children by default. It does this even if the props passed to them are identical.
Re-rendering is not the same as updating the screen
A re-render only runs your function and compares the result with the previous one. React then changes the real DOM only where the output differs. So most re-renders are cheap and produce no visible work.
This is why re-renders are usually fine. Rendering is fast, and React is designed to do it often.
When it starts to matter
Re-renders become a problem when the render work is large or frequent. Typical cases:
- A list with thousands of rows where each row does some computation.
- An expensive calculation, such as filtering or sorting a big array, repeated on every keystroke.
- A slow child that re-renders every time an unrelated piece of parent state changes.
Here is a component that shows the pattern:
import { useState } from 'react';
function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
}
export function List({ items }) {
const [query, setQuery] = useState('');
const [selectedId, setSelectedId] = useState(null);
const visible = items.filter((i) => i.name.includes(query));
const handleSelect = (id) => setSelectedId(id);
return (
<>
<input value={query} onChange={(e) => setQuery(e.target.value)} />
<p>Selected: {selectedId}</p>
<ul>
{visible.map((item) => (
<Row key={item.id} item={item} onSelect={handleSelect} />
))}
</ul>
</>
);
}Clicking a row calls setSelectedId. List re-renders, and so does every Row, even though the only visible change is the "Selected" text. With 5,000 items, that is 5,000 wasted function calls per click.
Unstable references
Skipping re-renders needs a way to ask "did the props change?" React answers with a shallow comparison using Object.is. For numbers and strings, equal values compare as equal. For objects, arrays and functions, only the same reference compares as equal.
In the example above, handleSelect is defined inside List. Each render creates a new function. Two functions with identical code are still different values:
const a = () => 1;
const b = () => 1;
console.log(a === b); // falseSo even if you wrapped Row in React.memo, which skips a render when props are shallow-equal, onSelect would be a new function every time. The memo check would fail and Row would re-render anyway. The same happens with object and array literals created during render, such as style={{ color: 'red' }}.
This is the core problem: skipping work needs both a memo boundary and stable references. Keeping references stable is what useMemo and useCallback are for. Writing them by hand is the topic of the next chapter.
Check before you optimize
Don't memoize by default. Measure first.
- Open React DevTools Profiler and record the slow interaction.
- Look for components that rendered but whose output did not change.
- Optimize only the ones that take noticeable time.
A render that takes 0.2 ms needs no help. A render that blocks typing for 100 ms does.
Exercise
In the List example, a user clicks a row and selectedId changes. Which components re-render, and why would wrapping Row in React.memo alone not stop its re-renders?
Show answer
List re-renders because its state changed, and every Row re-renders because its parent did. React.memo alone would not help because handleSelect is recreated on each render. Its reference differs every time, so the shallow props comparison fails. The function would also need a stable reference, for example through useCallback.
Exercise
Is Object.is({ a: 1 }, { a: 1 }) true or false, and why does this matter for memoization?
Show answer
False. Two separate object literals are different references even when their contents match. Props that are objects, arrays or functions created during render are new values on every render, so a shallow comparison treats them as changed.