The Wayback Machine - https://web.archive.org/web/20240730103045/https://www.geeksforgeeks.org/what-is-a-pure-functional-component-in-reactjs/
Open In App

What is a pure functional component in ReactJS ?

Last Updated : 19 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

What is a JavaScript function?

A JavaScript function is a block of code designed to perform a particular task which is executed when it is called.

How React functional components are similar to JavaScript functions?

Conceptually, components are like JavaScript functions.  A functional component is a JavaScript function that returns JSX in React. These functions accept arbitrary inputs (often called “props”) and return React elements describing what should appear on the screen.

 

When a React Component is Pure?

A function is said to be pure if the return value is determined by its input values only and the return value is always the same for the same input values.

A React component is said to be pure if it renders the same output for the same state and props. For React pure class components, React provides the PureComponent base class. Class components that extend the React.PureComponent classes are treated as pure components.

Create React Application:

  • Step 1: Create a React application using the following command:

    npx create-react-app pure-react
  • Step 2: After creating your project folder i.e. pure-react, move to it using the following command:

    cd pure-react

Project Structure: It will look like the following.

Filename: App.js

App.js




App.js import React from 'react';
import GeeksScore from './geekscore';
  
export default function App() {
  return (
    <div>
      <GeeksScore score="100" />
    </div>
  );
}


geekscore.js




import React from 'react';
  
class GeeksScore extends React.PureComponent {
  
  render() {
    const { score, total = Math.max(1, score) } = this.props;
  
    return (
      <div>
        <h6>Geeks Score</h6>
        <span>{ Math.round(score / total * 100) }%</span>
      </div>
    )
  }
  
}
  
export default GeeksScore;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Why Pure Components?

Pure components have some performance improvements and render optimizations because React implements the shouldComponentUpdate() method for them with a shallow comparison for props and state.

What is a pure functional component in React?

Functional components are very useful in React, especially when we want to isolate state management from the React component. That’s why they are often called stateless components.

However, functional components cannot leverage the performance improvements and render optimizations that come with React.PureComponent since they are not classes by definition.

Optimizing a functional component, so that React can treat it as a pure component shouldn’t necessarily require that the component be converted to a class component. To create a pure functional component in React, React provides a React.memo() API. Using the React.memo() API, the React functional component can be wrapped as follows to get React Pure Functional Component.

Some important things about React.memo() API are:

  • React.memo() is a higher-order component that takes a React component as its first argument and returns a pure React component.
  • React component type returned using React.memo() allows the renderer to render the component while memoizing the output.
  • React.memo() also works with components w using ReactDOMServer.

App.js




import React from 'react';
import GeeksScore from './geekscore';
  
export default function App() {
  return (
    <div>
      <GeeksScore score="100" />
    </div>
  );
}


geekscore.js




import React, { memo } from 'react';
  
function GeeksScore({ score = 0, total = Math.max(1, score) }) {
  return (
    <div>
      <h2>Geeks Score</h2>
      <span>{ Math.round(score / total * 100) }%</span>
    </div>
  )
}
  
export default memo(GeeksScore);


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:



Similar Reads

How does a pure component differ from a functional component?
React provides two main types of components: pure components and functional components. Understanding the differences between these two types is important for effective React development. In this article, we will learn about pure components and functional components, along with discussing the significant distinction that differentiates between pure
4 min read
How to convert functional component to class component in ReactJS ?
ReactJS offers two primary ways of creating components: functional components and class components. While functional components are more concise and easier to reason about, there are situations where class components may be necessary with the flexibility to use lifecycle methods and manage the state in a different way. PrerequisitesReact JSReact Fu
2 min read
Functional Programming: Pure and Impure Functions
Functional programming is a programming paradigm that focuses on the use of functions to solve problems. In functional programming, functions are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned as values. Pure and impure functions are two important concepts in functional programming that
4 min read
How to access props.children in a stateless functional component in ReactJS ?
The {props. children} allows you to create a generic template, which can be modified by a parent when invoked. In this way, a parent component can easily pass whatever is necessary to its child, even generated layout features. Anytime you call a component, it automatically displays whatever it has between its opening and closing tags. A component w
5 min read
How to get previous state in ReactJS Functional Component ?
To get the previous state in ReactJS Functional Component we can use the previous value of the state while using the set state method. State can only be directly accessed in the class component so we will be using the react useState hook to accomplish the task. PrerequisitesReact JSReact JS functional componentsReact useState HookSteps to create Re
2 min read
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. Table of Content Components in ReactRegular ComponentsBenefits of Regular ComponentsWhen to use Regular Compo
4 min read
How to change states with onClick event in ReactJS using functional components ?
In modern web applications, to change states with onClick event in React JS using functional components is done by Hooks in React as the functional components don't have inbuilt states like class components. With Hooks, state objects are completely independent of each other, so you can have as many state objects as you want. In a single-page applic
2 min read
ReactJS Functional Components
Functional Component is one way to create components in a React Application. React.js Functional Components helps to create UI components in a Functional and more concise way. In this article, we will learn about functional components in React, different ways to call the functional component, and also learn how to create the functional components.
5 min read
How to use PropTypes in a Functional Component in React?
PropTypes is a mechanism provided by React for type-checking props passed to components. It allows developers to specify the expected types of props (such as strings, numbers, arrays, objects, etc.) and whether they are required or optional. PropTypes helps catch bugs early by providing warnings in the browser console when incorrect prop types are
2 min read
How to access props inside a functional component?
React is an open-source JavaScript library that is mainly used for developing User Interface or UI components. It is a single-page application that is popularly used for developing dynamic web interfaces. While building a React application, the React components serve as basic building blocks. In React, web applications are built by the combination
3 min read
three90RightbarBannerImg