← React Beginner Questions - Set 4 Q38
React Beginner Questions - Set 4
A class component with a built-in shallow `shouldComponentUpdate`. The manual version shows exactly what it does and what it misses.
import { Component, PureComponent } from 'react';
// What PureComponent does for you
class Manual extends Component {
shouldComponentUpdate(nextProps) {
return nextProps.value !== this.props.value; // shallow, one field
}
render() { return <p>{this.props.value}</p>; }
}
class Pure extends PureComponent {
render() { return <p>{this.props.value}</p>; }
}
// Both skip the render when the shallow comparison is equal
<Pure value={{ n: 1 }} /> {/* new object each render: always re-renders */}