Sub topic 1
Why components re-render
Prerequisites
This chapter assumes you know:
- How to write function components and JSX.
- How
useStateanduseEffectwork. - How props and
childrenpass between components. - That JavaScript objects, arrays and functions are reference values.
Rendering and committing
In React, rendering means calling your component function to find out what it should display. The function returns a description of the UI. React compares that description with the previous one and updates the DOM only where they differ. That DOM update step is called the commit.
A re-render is any later call to the component function. A re-render does not always change the DOM. React can call your function, find the output identical, and commit nothing. The work of calling the function and comparing the output is still spent, and that is the cost this tutorial is about.
What triggers a re-render
Three things cause a component to render again:
- Its own state changes. Calling a state setter with a new value schedules a render of that component.
- Its parent re-renders. When a component renders, React renders every child it returns, by default.
- A context it reads changes. Components that call
useContextfor a context re-render when its value changes.
A change in props is not on this list. Props change because a parent re-rendered, and the parent re-render is the trigger. A child re-renders even when you pass it exactly the same props.
A parent re-render reaches every child
This example shows the second trigger. Child takes no props and never changes, yet it renders on every click.
import { useState } from 'react';
function Child() {
console.log('render Child');
return <p>I never change.</p>;
}
export function Parent() {
const [count, setCount] = useState(0);
console.log('render Parent');
return (
<>
<button onClick={() => setCount(count + 1)}>Clicked {count}</button>
<Child />
</>
);
}Click the button three times and the console shows render Parent and render Child three more times each. The click changed count, so Parent rendered. Parent returns <Child />, so Child rendered too.
The cascade runs downward only. A state change in Child never re-renders Parent. It re-renders Child and the components Child returns.
When React skips the work
React does not render when it can prove nothing changed. Two rules matter:
- Same state value. If you call a setter with a value equal to the current one, compared with
Object.is, React usually skips the render. CallingsetCount(0)whilecountis already0does nothing visible. - New object, same contents. A setter called with
{ a: 1 }when the state is a different{ a: 1 }object counts as a change. The two objects are different references, so React renders. Later chapters cover this in detail.
A component returning identical output does not stop its children from rendering. React only skips a subtree when it can avoid calling the component function in the first place.
Common mistakes
- Treating every re-render as a bug. Rendering is cheap for most components. Re-renders only matter when they are slow enough for users to notice.
- Thinking a prop change is the trigger. Passing unchanged props does not stop a render. Skipping renders by props needs
React.memo, which a later chapter covers. - Mutating state. Pushing into an array held in state and calling the setter with the same array gives React the same reference. React sees no change and may skip the update. Always pass a new array or object.
- Expecting a render to mean a DOM update. A render is a function call. The DOM changes only at commit, and only where the output differs.
Where this leads
Once you know what triggers a render, you have three kinds of fix when one is too slow. You can skip the render, make it cheaper, or stop it from being triggered at all. The next chapters take these in order, starting with how to measure the problem before changing any code.
Exercise
In the Parent and Child example, you wrap <Child /> in nothing and change setCount(count + 1) to setCount(count). After the first click, does Child render again on later clicks? Why or why not?
Show answer
No. The setter receives a value equal to the current state, as compared by Object.is, so React skips rendering Parent. Because Parent does not render, Child is not rendered either. React may render Parent one extra time before it bails out, but it does not commit changes or cascade further.