The Wayback Machine - https://web.archive.org/web/20240114143412/https://www.geeksforgeeks.org/reactjs-pure-components/
Open In App
Related Articles

ReactJS Pure Components

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Generally, In ReactJS, we use shouldComponentUpdate() Lifecycle method to customize the default behavior and implement it when the React component should re-render or update itself.

Prerequisite: 

React JS Pure Components :

Now, ReactJS has provided us with a Pure Component. If we extend a class with Pure Component, there is no need for shouldComponentUpdate() Lifecycle Method. ReactJS Pure Component Class compares the current state and props with new props and states to decide whether the React component should re-render itself or  Not.

In simple words, If the previous value of the state or props and the new value of the state or props are the same, the component will not re-render itself. Pure Components restricts the re-rendering when there is no use for re-rendering of the component. Pure Components are Class Components that extend React.PureComponent. 

Example: This example demonstrates the creation of Pure Components. 

Javascript




import React from "react";
 
export default class Test extends React.PureComponent {
    render() {
        return <h1>Welcome to GeeksforGeeks</h1>;
    }
}


Output :

Pure Components Key Points:

  • Shallow Comparison:
    • Pure components perform a shallow comparison of the props and states. If the objects are passed as props or state have the same references, a re-render is prevented.
  • Performance Optimization:
    • Pure components can provide performance optimizations by preventing unnecessary re-renders when the data is same and hasn’t modified.
  • ShouldComponentUpdate:
    • Pure components automatically implement the shouldComponentUpdate() method with a shallow prop and state comparison. This method returns false if the props and state haven’t changed.

Extending React Class Components with Pure Components ensures the higher performance of the Component and ultimately makes your application faster, While in the case of Regular Component, it will always re-render either value of State and Props changes or not.

While using Pure Components, Things to be noted are that, In these components, the Value of State and Props are Shallow Compared (Shallow Comparison) and It also takes care of “shouldComponentUpdate” Lifecycle method implicitly.
So there is a possibility that if these State and Props Objects contain nested data structure then Pure Component’s implemented shouldComponentUpdate will return false and will not update the whole subtree of Children of this Class Component. So in Pure Component, the nested data structure doesn’t work properly.

In this case, State and Props Objects should be simple objects and Child Elements should also be Pure, means to return the same output for the same input values at any instance.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 28 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials