Rendering is React calling your component to find out what the UI should look like. React treats that call as a pure calculation: given the same props, state and context it must return the same elements, and it must not change anything that existed before the call: module variables, props, state objects, the DOM, a network request.
Why React needs it. React doesn't promise one call per update. It may render a component and throw the result away (an interrupted transition, a Suspense retry), render it twice (Strict Mode), skip it because its inputs didn't change (memo, the React Compiler), or render it on the server. Each of those is only safe if calling the component has no effect beyond its return value.
What's allowed. Local mutation is fine: building a new array and pushing into it during render changes nothing anyone else can see. Lazy initialisation (a useState initializer) is fine. Side effects go in event handlers, or in effects as a last resort; they run outside rendering.
How Strict Mode catches violations. Inside <StrictMode>, development builds call these functions twice:
- the component body (not event handlers);
- functions passed to
useState,useReduceranduseMemo, and the reducer and updater functions; - some class methods, such as
constructor,renderandshouldComponentUpdate.
A pure function returns the same thing twice, so nothing changes. An impure one shows the difference: an item pushed twice, a counter that skips numbers. React keeps one result and ignores the other.
Strict Mode also runs an extra effect setup and cleanup cycle, and an extra ref callback setup and cleanup on mount; those test cleanup, not purity. None of it happens in production builds.
Double rendering only catches impurity that changes the result between two back-to-back calls.