Keep render functions pure
Make rendering a predictable calculation
A pure render describes UI without changing shared data or performing work outside that calculation. For matching props, state and context, it should produce an equivalent UI description. That does not require the exact same JavaScript object reference. React may invoke a component more than once, restart rendering or discard a result; rendering must not depend on how often the function runs.
let ticket = 0;
function TicketLabel() { ticket += 1; return <p>Ticket {ticket}</p>;}
export default function App() { return <main><TicketLabel /><TicketLabel /></main>;}This is valid JSX syntax but incorrect component behavior. Every TicketLabel invocation increments a variable that existed before rendering. Output depends on previous calls. A simple sequential invocation could produce Ticket 1 and Ticket 2, but React does not promise that exact sequence or a specific number of invocations. The two labels must not allocate real ticket numbers by rendering. Do not copy this example into a normal application.
Pass the data into the calculation
function TicketLabel({ number }) { return <p>Ticket {number}</p>;}
export default function App() { return ( <main> <h1>Reserved tickets</h1> <TicketLabel number={1} /> <TicketLabel number={2} /> </main> );}Expected display: Reserved tickets, Ticket 1 and Ticket 2. A prop is an input passed to a component. Here { number } extracts the number input; this small preview is explained fully in Chapter 6. TicketLabel only reads the number and produces the paragraph. Repeating the calculation with number 1 yields an equivalent label, without updating shared state. Real ticket allocation would belong to a deliberate action or an authorized backend, not to the render function.
Local mutation is different from shared mutation
const supplies = ['Paper', 'Brush', 'Ink'];
export default function App() { const ordered = [...supplies]; ordered.sort(); return ( <main> <h1>Supply checklist</h1> <p>{ordered.join(', ')}</p> <p>Original: {supplies.join(', ')}</p> </main> );}Expected first paragraph: Brush, Ink, Paper. The second says Original: Paper, Brush, Ink. Array sort changes the array on which it is called. Spreading the source into a fresh array allows local sorting without changing supplies. This is safe here because the array contains strings. A shallow copy does not protect nested objects. Do not call supplies.sort() directly in render or change incoming props.
Separate render from external work
Do not send requests, write to storage, register listeners or allocate real resources as an incidental result of rendering. User actions belong in event handlers; external synchronization belongs in the appropriate effect or framework facility, taught later. Reading the current clock or random numbers inside render can make identical inputs yield inconsistent UI. Development StrictMode can expose impurity by repeating rendering, but passing one development run is not proof of purity; production rendering also has no fixed invocation-count guarantee.
Practice
Repair a component that calls sort on a shared array. Explain which value changes in the bad version and why a fresh copy preserves the original. Then predict repeated results from the repaired version.