Sub topic 2
Rendering is a pure calculation of UI from props and state
This chapter assumes you know JavaScript functions, objects, and arrays, basic React components with props and JSX, the useState hook, and the browser DOM. It covers what "rendering" means in React and the rule that makes the rest of the model work.
Rendering means calling your component
In React, rendering is React calling your component function to find out what the screen should look like. It does not mean drawing pixels or touching the DOM. The function returns a description of the UI, made of React elements. React uses that description later to decide what to change in the DOM.
The model is a formula:
UI = component(props, state)For the same props and state, a component should return the same description. That is what "pure" means here.
What purity requires
A pure render does two things:
- It reads only its inputs: props, state, and context.
- It produces no side effects while running. It does not change variables outside the component, write to the DOM, or start network requests.
Creating and changing values that are local to the render is fine. A new array built inside the function and then mutated there is safe, because no other render can see it.
Watching a render happen
The counter below logs on every call. The log runs during the render, before React changes anything on screen.
import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
console.log('Counter rendered with count', count);
return (
<button onClick={() => setCount(count + 1)}>
Clicked {count} times
</button>
);
}Each click calls setCount, which schedules a new render. React calls Counter again with the new state, and count holds the new value during that call. A render is a snapshot: count never changes partway through one.
Common errors
Each of these breaks purity. Each can produce a UI that disagrees with its inputs.
let renderCount = 0;
function Bad({ items }) {
renderCount++; // changes a variable outside the component
items.push('extra'); // mutates a prop owned by the parent
document.title = 'Hi'; // writes outside React during render
return <p>{items.length}</p>;
}A corrected version leaves the inputs alone and moves the side effect out of the render body:
import { useEffect } from 'react';
function Good({ items }) {
const shown = [...items, 'extra']; // a new local array
useEffect(() => {
document.title = 'Hi';
}, []);
return <p>{shown.length}</p>;
}Other common mistakes include reading Math.random() or Date.now() directly in the returned UI, which gives different output for the same inputs. Another is mutating state in place, for example state.push(x), instead of setting a new array.
Why React insists on this
Because rendering is pure, React can treat it as a calculation it is free to schedule. It can skip a render, repeat one, or pause one and resume it later. These abilities matter for batching, memoization, and concurrent rendering, covered in later chapters. In development, Strict Mode calls your components twice to expose impure code. A component whose output changes between those two calls has a bug.
Side effects belong in event handlers, which run in response to a user action, or in useEffect, which runs after React has updated the screen.
Exercise
A component contains const id = Math.random(); in its body and renders <p>{id}</p>. Why does this violate the purity rule, and what is one way to fix it?
Show answer
Calling Math.random() during render gives different output for the same props and state, so the UI is not a pure function of its inputs. Every re-render shows a new number. To fix it, generate the value once with useState(() => Math.random()), or create it in an event handler or an effect and store it in state.