? Performance Optimization
A Pure Component is a special kind of React component that automatically skips unnecessary re-renders by doing a shallow comparison of its props and state.
React.PureComponent for class components and React.memo() for functional components.React.PureComponent and skips rendering when props/state are shallowly equal.React.memo() wraps a functional component and gives PureComponent-like behavior.In a normal React Component, the child re-renders whenever the parent re-renders, even if the received props are exactly the same.
A Pure Component (via React.PureComponent) automatically checks previous vs new props/state with a shallow comparison and only re-renders when something actually changes.
// Regular class component - always re-renders when parent renders
import React, { Component, PureComponent } from "react";
class RegularComponent extends Component {
render() {
console.log("Regular Component rendered");
return <h3>Regular: {this.props.name}</h3>;
}
}
// Pure component - skips render when props/state are shallowly equal
class PureComp extends PureComponent {
render() {
console.log("Pure Component rendered");
return <h3>Pure: {this.props.name}</h3>;
}
}
export { RegularComponent, PureComp };
Here is a parent component that updates state with the same value every 2 seconds. This lets us see how Regular vs Pure Components behave.
// Parent component to compare RegularComponent and PureComp
import React, { Component } from "react";
import { RegularComponent, PureComp } from "./ChildComponents";
class ParentComp extends Component {
constructor(props) {
super(props);
this.state = { name: "React" };
}
componentDidMount() {
setInterval(() => {
// State is updated with the SAME value - no "real" change
this.setState({ name: "React" });
}, 2000);
}
render() {
console.log("Parent rendered");
return (
<div>
<h2>Parent Component</h2>
<RegularComponent name={this.state.name} />
<PureComp name={this.state.name} />
</div>
);
}
}
export default ParentComp;
ParentComp calls setState with the same value.name is unchanged.name.In the browser console you will see multiple logs:
"Parent rendered" every 2 seconds."Regular Component rendered" every 2 seconds."Pure Component rendered" only for the first render (and whenever props actually change).React.PureComponent implements shouldComponentUpdate() with a shallow comparison.
// Bad pattern: mutating nested state object in-place
this.state = { user: { name: "Alex" } };
// Later in code
const user = this.state.user;
user.name = "Sam"; // Same object reference - shallow compare thinks "no change"
this.setState({ user }); // PureComponent may SKIP the re-render here
For functional components, the equivalent optimization is React.memo(). It wraps a component and re-renders it only when its props change (again, using shallow comparison by default).
// Functional child wrapped with React.memo for Pure-like behavior
import React from "react";
const Child = ({ value }) => {
console.log("Child rendered");
return <p>Value: {value}</p>;
};
const MemoizedChild = React.memo(Child);
export default MemoizedChild;
When you use MemoizedChild, React will only re-render it if the value prop changes (based on shallow comparison). If the parent re-renders without changing value, the memoized child is skipped.
React.PureComponent for class components that depend heavily on props/state.React.memo() for functional components needing the same optimization.ParentComp with both RegularComponent and PureComp.setInterval to repeatedly call setState with the same value.React.memo().Goal: Understand how React.PureComponent and React.memo() skip unnecessary re-renders and improve React performance.