React components are the building blocks of a React application, representing different parts of the user interface. In React, there are four types of components. We will discuss pure components in this answer.
Pure components are a specialized type of components in React that optimize rendering performance by implementing a shallow comparison of props and state. They do not re-render when the value of state and props has been updated with the same values. They are an alternative to regular components that extend the React.Component
class. Pure components extend the React.PureComponent
class instead.
PureComponent
is exactly the same as Component
except that it handles the shouldComponentUpdate
method for you.
When a pure component receives new props or its state is updated, React automatically performs a
Note: Use pure react children components inside your pure component.
Performance optimization: Pure components minimize re-renders by avoiding unnecessary rendering cycles when there are no changes in props or state. This is more helpful in cases where expensive calculations or API calls are involved.
Simplified code: Pure components simplify your code by reducing the need for manual checks and optimizations. React handles the comparison logic automatically.
Easy integration: Pure components can replace regular components in your code. You can easily convert a component to a pure component by changing the base class from React.Component
to React.PureComponent
.
To illustrate how pure components work, let's consider a simple example. We will compare the implementation of a regular component and a pure component.
Every time the component receives new props, it will re-render regardless of whether the props have actually changed.
To see the console like the above, right-click the mouse and then select inspect. You will see the panel as shown above, having different tabs elements, console, sources, etc. Select the console tab.
The pure component will only re-render if there are differences in the props or state values as shown below. As we are setting the counter value to the same value (0), it is not re-rendering.
Now, let's see the explanation of real component and pure component examples and find the difference between their codes.
Line 3: This line defines a class component called App
that extends the Component
class from React.
Lines 4–9: This block defines the constructor method of the App
component. It initializes the state of the component with a property counter
set to 0
.
Lines 11–18: The componentDidMount
method is called immediately after the component is mounted. It sets up an interval that invokes a setState
method is called with an object that has a counter
property set to this.state.counter
. However, this does not update the state because the value is not modified.
Lines 20–22: The componentDidUpdate
method is called whenever the component updates and re-renders. It logs 'App updated' to the console.
Lines 24–31: The render
method is responsible for rendering the component's UI. It logs 'Render invoked' to the console.
Line 4: This line defines a class component called App
that extends the PureComponent
class from React.
The rest of the code is the same as the regular component.
Immutable data: To ensure the proper functioning of pure components, it is important to use immutable data structures for props and states. Immutable data ensures that a new object or array is created whenever there are changes, which enables accurate shallow comparisons.
Nested objects or arrays: Shallow comparisons in pure components may not work as expected for deeply nested objects or arrays. In such cases, you should consider using immutable data libraries or do implement custom shouldComponentUpdate
logic.
Pure components offer a valuable optimization technique in React by reducing unnecessary re-renders. They automatically perform shallow comparisons of props and state, resulting in improved performance and simplified code.
Free Resources