Open In App

Explain the scenario where using a PureComponent is advantageous.

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

React PureComponent is a supercharged component that automatically checks for an update before rendering. It is ideal for components that heavily rely on props and state. It achieves this by comparing incoming facts with what it already holds, avoiding unnecessary renderings and thus saving considerable processing power. It makes your app faster especially when you have so many components to display or frequent updates.

PureComponent is like an in-built tool for optimization that helps to keep your application running well without much additional work.

We will discuss about the approach for creating pure components:

Advantages of Using PureComponent in React:

  • Performance Optimization:
    • PureComponent performs a shallow comparison of the current and next props and state. If there are no changes, it prevents unnecessary re-renders.
    • This is especially useful in large and complex applications where rendering a component can be resource-intensive.
  • Props and State Immutability:
    • To fully leverage the benefits of PureComponent, ensure that both props and state are immutable. Shallow comparisons can quickly identify changes only if the references to props or state objects differ.
  • Complex UI Components:
    • Components with a complex UI or numerous child components can benefit from PureComponent. In such cases, preventing unnecessary re-renders helps maintain a smooth user experience.
  • Avoiding Unnecessary Side Effects:
    • If a component contains side effects in lifecycle methods (e.g., data fetching, subscriptions), avoiding unnecessary re-renders helps minimize the number of times these side effects are triggered.
  • Props Drilling Scenarios:
    • In scenarios where props are drilled down through multiple levels of components, using PureComponent can prevent unnecessary re-renders if the incoming props have not changed.

1. Class Component Approach:

  • When working with class components, particularly when aiming for optimization through avoiding unnecessary re-renders, you can extend the React.PureComponent class.
  • Extend React.PureComponent class and implement the render method. To optimize rendering, PureComponent internally handles prop and state comparison.
import React, {
PureComponent
} from 'react';

class MyComponent extends PureComponent {
render() {
return <div>{this.props.data}</div>;
}
}

2. Functional Component with React.memo():

  • As such, a functional component will be used here and protected using React.memo() for the same optimization performance
  • It wraps a functional component into React.memo(), which check props shallowly so as to re-render the component.
import React from 'react';

const MyComponent = React.memo(({ data }) => {
return <div>{data}</div>;
});

3. Using React.PureComponent in a Functional Component:

  • This converts a functional component to memoized component by use of React.memo(). This then optimizes rendering by preventing un-necessary re-renders of components whose props remain unchanged.
import React from 'react';

const MyComponent = ({ data }) => {
return <div>{data}</div>;
};

export default React.memo(MyComponent);

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads