Sub topic 1
Rendering should be pure
Prerequisites
This chapter assumes you know function components and JSX, props, and the useState hook. You should also know that a component re-renders when its state changes. No knowledge of useEffect is needed yet. This chapter explains why the hook exists.
What "pure" means
A component is pure when its output depends only on its inputs. The inputs are props, state, and context. Given the same inputs, it must return the same JSX. It must also leave the world outside the component unchanged while it runs.
React relies on this. It may call your component function several times, pause a render, or throw a render away before it reaches the screen. If rendering does extra work, such as changing a variable outside the function, that work happens an unpredictable number of times.
The render and commit cycle
React updates the screen in phases:
- Trigger: state changes or a parent re-renders.
- Render: React calls your component functions to work out what the screen should look like. This phase must be pure.
- Commit: React applies the differences to the DOM.
- After commit: the browser paints, and React runs effects.
Work that changes something outside the component belongs in the last phase, not in the render phase.
An impure component
This component changes a variable that lives outside it:
let guestCount = 0;
function Guest() {
guestCount = guestCount + 1; // changes something outside the component
return <h2>Guest #{guestCount}</h2>;
}
export default function Party() {
return (
<>
<Guest />
<Guest />
<Guest />
</>
);
}The result depends on how many times React has called Guest, not on its props. If React renders the tree twice, the numbers become 4, 5 and 6. The fix is to pass the value in:
function Guest({ number }) {
return <h2>Guest #{number}</h2>;
}
export default function Party() {
return (
<>
<Guest number={1} />
<Guest number={2} />
<Guest number={3} />
</>
);
}Guest now returns the same JSX for the same props, however often React calls it.
What is allowed during render
Pure does not mean "no variables." You can create and change values that are local to the render:
function List({ names }) {
const items = []; // created during this render
for (const name of names) {
items.push(<li key={name}>{name}</li>);
}
return <ul>{items}</ul>;
}Changing items is safe because it is created fresh on every call, so nothing else can see the change. Do not change props or state objects directly, and do not write to anything that existed before the render began.
Where does impure work go?
Real apps must do impure things. They update the page title, fetch data, start timers, and listen for browser events. These are side effects, and the next chapter defines them precisely. React gives you two places to put them:
- Event handlers, which run when the user does something, such as a click.
- Effects, declared with
useEffect, which run after React commits the render.
Exercise
A component contains the line window.visits = window.visits + 1; in its body, outside any handler or effect. Why is this a problem, and where could the work move?
Show answer
Rendering must not change anything outside the component. React can call the function more than once per update, so the counter would over-count. The change belongs in an event handler if a user action causes it, or in an effect if it must happen after the component appears on screen.