React Reference ยท Components and Special Elements
React PureComponent class
Reference for React PureComponent: skip re-renders when props and state are shallowly equal, avoid the mutation pitfall, and compare it with memo.
PureComponent is a variant of Component that implements shouldComponentUpdate for you. Before re-rendering, React compares the new props and state with the old ones shallowly. If every top-level value is identical (compared with Object.is), the component skips the render.
That makes it a cheap optimization for class components whose output depends only on their props and state. It is the class equivalent of wrapping a function component in memo. The catch is the word "shallowly": it compares references, not contents, so it only works when you update data immutably.
import { PureComponent } from 'react'; class Row extends PureComponent { render() { return <li>{this.props.label}</li>; } }
Try it
In the first example the console shows both rows rendering once on mount. After you click the parent button, PlainRow renders again each time and PureRow never does, because its single prop is the same string. In the second example, Push x appears to do nothing: the array is the same reference, so neither App nor List re-renders. Add y creates a new array, and the screen finally catches up and shows all three entries.
Syntax
class Name extends PureComponent { render() { /* ... */ } }
PureComponent has the same members as Component (props, state, setState, forceUpdate, lifecycle methods). The one difference is the built-in comparison:
| Compared | How |
|---|---|
| Props | Every own key of old and new props, using Object.is. |
| State | Every own key of old and new state, using Object.is. |
If everything matches, render is skipped, and the lifecycle methods after it (componentDidUpdate) do not run either.
Do not define shouldComponentUpdate on a PureComponent. React warns about it.
Usage notes
- Always update immutably. Use
[...items, x],{ ...obj, key: value },mapandfilter. Mutating and passing the same reference defeats the check. - Inline objects and functions break it.
<Row style={{ color: 'red' }} onClick={() => go()} />creates new references on every parent render, so the child re-renders anyway. Hoist constants or use bound class fields. - Children count as props.
<Pure><span /></Pure>receives a newchildrenelement each time, so it re-renders. - Shallow means shallow. Two arrays with equal contents but different identities count as different. Deep comparisons are expensive and are what
PureComponentavoids. - Not a guarantee. Treat the skip as a performance optimization, not a promise that render will never run.
- Measure first. Skipping renders also costs comparisons. It pays off for components that are expensive or that re-render with unchanged props very often.
- Modern equivalent. For function components use
memo, which also accepts a custom comparison function. The React Compiler can do much of this automatically. - Context ignores it. A
PureComponentthat reads context still re-renders when that context value changes.
Related
- Component, memo, Class lifecycle methods
- Lessons: Pure components, Memo