Open In App

How does a Pure Component Differ from a Regular Component ?

In React, components are the building blocks of user interfaces. They encapsulate the UI logic and render the UI elements based on the input data. React offers different types of components, including regular components and Pure Components.

Components in React

In React, components are JavaScript functions or classes that return a piece of UI. They can be divided into two main categories: functional components and class components.

Regular Components

Regular components in React, whether functional or class-based, re-render whenever their parent component re-renders or when their own state or props change. They don't perform any checks to determine whether their output should change, which can lead to unnecessary re-renders and decreased performance.

Syntax:

// Syntax for Regular component
import React, { Component } from 'react';

class YourComponent extends Component {
render() {
return
<div>
Your Creativity goes here.....
</div>;
}
}

Benefits of Regular Components

When to use Regular Components?

Example:

import React, { Component } from 'react';

class RegularComponent extends Component {
  render() {
    return <div>Regular Component</div>;
  }
}

Output:

Screenshot-2024-03-23-005203

Pure Component

Pure Components are a subclass of class components provided by React. They come with a built-in optimization to prevent unnecessary re-renders. Pure Components implement a shouldComponentUpdate method, which performs a shallow comparison of the component's props and state. If the new props and state are shallowly equal to the previous props and state, the component won't re-render, thereby optimizing performance.

Syntax:

// Syntax for Pure component
import React, { PureComponent } from 'react';

class YourComponent extends PureComponent {
render() {
return
<div>
Your Creativity goes here......
</div>;
}
}

Benefits of Pure Components

When to use Pure Components?

Example:

import React, { PureComponent } from 'react';

class Pure extends PureComponent {
  render() {
    return <div>Pure Component</div>;
  }
}

Output:

Screenshot-2024-03-23-005026Difference

FeatureRegular ComponentsPure Components
Performance OptimizationDo not optimize performance by default.Optimize performance by preventing unnecessary re-renders.
ImplementationMust manually implement shouldComponentUpdate for optimization.Automatically implement shouldComponentUpdate for optimization.
Re-rendering BehaviorRe-renders whenever its parent re-renders or when props/state change.Re-renders only when props/state change with shallow comparison.
ComplexityMay require additional logic to prevent unnecessary re-renders.Simplifies logic by automatically handling re-rendering optimization.
Ease of UseOffers flexibility but requires manual optimization.Offers built-in optimization without additional configuration.

Conclusion

In summary, Pure Components and regular components differ in their re-rendering behavior and built-in optimization for preventing unnecessary re-renders. Regular components offer flexibility and control, while Pure Components optimize performance and simplify rendering logic. Understanding the differences between these two types of components allows developers to make informed decisions about when to use each one based on the specific requirements of their React applications.

Article Tags :