Sub topic 1
Why Components Re-render and Why It Sometimes Matters
Prerequisites
Assume you know React function components, props, state, useMemo, and useCallback. You should also understand that React re-renders a component when its state changes or when its parent re-renders.
Why Components Re-render and Why It Sometimes Matters
A re-render means React calls a component function again to calculate its next user interface. It does not necessarily mean that the browser changes the DOM. React compares the new result with the previous result and updates only what changed.
Common causes include:
- The component's state changes.
- Its parent re-renders.
- A context value it reads changes.
- Its props change.
Re-rendering is usually cheap and keeps React code simple. It matters when a component performs expensive calculations, renders a large list, or causes many child components to run. New object, array, and function values can also defeat shallow comparisons because each render creates a new reference.
flowchart TD
A[State, props, or context changes] --> B[Component function runs]
B --> C[React compares the result]
C --> D[Only necessary DOM updates]The Cost of Memoizing by Hand
Manual memoization uses memo, useMemo, and useCallback to preserve component results or references. It can help, but you must maintain dependency arrays correctly.
const Row = memo(function Row({ item, onSelect }) {
return <li onClick={() => onSelect(item.id)}>{item.name}</li>;
});
function List({ items }) {
const [query, setQuery] = useState('');
const visible = useMemo(
() => items.filter((item) => item.name.includes(query)),
[items, query]
);
const select = useCallback((id) => console.log(id), []);
return visible.map((item) => (
<Row key={item.id} item={item} onSelect={select} />
));
}Incorrect dependencies can produce stale values. Extra memoization can make code harder to read and can cost more than the calculation it avoids.
What the React Compiler Does
The React Compiler is a build-time tool. It analyzes React code and transforms eligible components and hooks so React can reuse calculations and skip unnecessary work. You write ordinary components, and the compiler adds suitable memoization during the build.
The compiler does not remove the need to design efficient components. It cannot safely optimize code whose behavior depends on mutations or hidden side effects.
The Rules of React the Compiler Depends On
Components must render purely: the same inputs should produce the same result, without changing external data. Props and state must be treated as immutable. Hooks must be called at the top level of components or custom hooks, in the same order on every render.
function Good({ items }) {
const sorted = [...items].sort();
return <ul>{sorted.map((item) => <li key={item}>{item}</li>)}</ul>;
}Good copies the prop before sorting it. Mutating items during render could make compiler optimizations incorrect.
Exercise
Should you add useCallback to every event handler?
Show answer
No. Add manual memoization when measurement shows a benefit or when an API requires stable references. The compiler can handle many ordinary cases.