Pure components are similar to react components but does not re-render if the props and state does not change
When a pure component receives new props and state data, it does a shallow comparison to check if the props or state data has changed.
If the data has not changed it does not re-render the component thus saving time and resources.
When re-rendering is time consuming and processing intensive it improves performance.
If you are looking for JavaScript Chat API and SDK for building chat in your app or website then DeadSimpleChat is the solution for you
What are react components
React components are like JavaScript functions. They let you split UI into different sections and think about a section of UI independently of others.
Components accept inputs called props and return react elements that display UI on the screen.
display component lifecycle methods here to increase the data
What are React functional components and React Class Components
React components can be defined as javascript functions as well as ES6 class.
React is leaning towards functional components and class components are being viewed as not best practice
Here is a basic example of react funtional component
This function accepts a prop (shorthand for properties) and returns a react element. Thus this is a react functional component
The function and class components both are equivalent in react's point of view.
What are Pure components in React
We have already seen what a pure component in react is in the introduction above.
To reiterate, pure components are components that do not re-render if the props and state have not changed
Pure components does a shallow check every time a new prop or state is supplied to it and skips the re-render if they have not changed thus saving precious time and resources required to re-render the component
How to use a Pure component
To use a Pure component extend Pure component instead of component in a class component
PureComponent
is a subclass of react component and as such has all the API's of the component class
In react a component re-renders when its parent re-renders, but as an optimization you can create a component that does not re-render when its parent re-renders as long as the props and state remains the same
Class component can opt into this by extending PureComponent
Let us use some of the examples to understand PureComponent
in action
Here we have a list of cars in a dealership. The CarList
takes an array of cars as a prop and lists the cars in an unordered list to visitors on the dealers website
The users going about exploring the website and new cars being added or subtracted is quite infrequent as the user is exploring the site
we can optimize the list of cars being displayed in a PureComponent
The CarList
component only relies on the property cars
which is an array
of cars and does not have any additional state
1. Table Component example
Here is another example of PureComponent
in react as a Table Component which renders a tab
This is a table that shows cars by their names, which car company they are from and their price
Since the Cars Component only relies on the data prop it can be extended as a PureComponent
Whenever, the parent components changes the Cars component checks if the prop data has changed or not if the prop data has not been changed the Cars component does not re-render
2. Button Component example
Here is an example of a button as a pure component. The button relies on a property to change text to Next or Restart based on the parent component
The NextOrRestart
class component returns a button and as it only relies on a prop and has no other complex logic it can be extended as a PureComponent
The NextOrRestart checks wheather the props has changed or not if the props have not changed the class does not re-render the button element
3. Chart Component example
The chart component renders its out put based on the data props. Since it does not have any other props or complex logic to depend upon. this class component can be rendered as a PureComponent
In htis example the PureChart
components checks whether the data prop has changed or not
of the data prop has not changed the component does not re render and only if the props has changed does the PureChart
component re-renders
Migrating Simple Components from Class to functional
React is moving towards functional components and if you have older class components you can convert them functional components.
If you want to convert PureComponents to functional components you need the React Memo API
React Memo API
Wrap a functional component in memo
to get a memoried version on that component.
This is useful when converting PureComponents which are class components to functional components
or when you want to create PureComponents as functional components
React memo API can be used like
React memo api is user to avoid re-rendering when the props remain the same and as such is a performance improvement tool
React components must render pure rendering logic. It means if the props, state and context remains the same it should always render the same out put
By using memo we are telling react that our component relies on this logic and react does not need to re render the prop if these remain the same
Let us see another simple example of using memo to convert a class component to a functional component using the memo
Here we have converted the PureComponent
which is a subset of class component to a functional component using the react memo
API
Updating the Memo component using state
When the component is memorized it will re-render when its state changes. memorization only has to do with the props that are passed on to it through its parent component
If the state variable is set to its current value the react will not re-render the component even without the memo.
This is a class component that uses state to store count. when handleclick
function is called the count increases.
What happens to memorized component when context changes
When a memorized component's context changes it will re-render. Memorized component only has to do with the props that are passed to it from its parent
let us look at an example
Note: Unlike PureComponent
the memo does not compare new state with previous state.
In functional components the set function with the same state already prevents the re-renders by default, even without the memo
You might be interested in some of our other articles
- Higher Order Components in React: Examples with Common Use-case Patterns
- memo vs useMemo in React
- React Context: The Detailed Guide
- React useState: The Complete guide
Conclusion
In this article we have learnt what are PureComponents
, difference between class and functional components, how to convert class components to functional components and how to convert PureComponents
to functional components
we also learnt about the react memo API. and what happens when state and context are used with the memo api
We also looked at a lot of examples with regards to react components and class component and functional components
I hope you have learnt from this article and it helps you in your journey.