Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recompose pure() vs React.PureComponent

What's the difference between pure() from the Recompose library and React.PureComponent? I'm guessing they are essentially solving the same problem. Can someone please clarify this?

like image 639
avatarhzh Avatar asked Sep 28 '18 01:09

avatarhzh


People also ask

What is the difference between React PureComponent and React component?

The difference between them is that React. Component doesn't implement shouldComponentUpdate() , but React. PureComponent implements it with a shallow prop and state comparison. If your React component's render() function renders the same result given the same props and state, you can use React.

Should I Update component or PureComponent?

It is the type of component which re-renders only when the props passed to it changes and not even if its parent component re-renders or if the shouldComponentUpdate()method is called. It is greatly used to enhance the performance of a web application.

What is PureComponent React?

A React component is considered pure if it renders the same output for the same state and props. For this type of class component, React provides the PureComponent base class. Class components that extend the React. PureComponent class are treated as pure components.

What is the difference between purecomponent and component in react?

When props or state changes, PureComponent will do a shallow comparison on both props and state. Component on the other hand won’t compare current props and state to next out of the box. Thus, the component will re-render by default whenever shouldComponentUpdate is called.

Can we use pure function for functional components in react?

For functional components we can use pure function as shown below − As PureComponent does shallow comparison of state and props objects and so there is a possibility that if these objects contains nested data structure then PureComponent’s implemented shouldComponentUpdate will return false .

What is shallow comparison in react pure component?

In case of React Pure Component, the changes in react “props” and “state” is Shallow Compared. We need to understand the concept of Shallow Comparison before we proceed.

How does pure component work?

It works only if the state and props are simple objects. Components can be termed as pure if they return same output for same input values at any point of time. If state or props references new object, PureComponent will re-render every time.


1 Answers

The difference is that React.PureComponent is stateful component and keeps track on the state:

React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

While Recompose is aimed at stateless functional components, pure shallowly detects changes in props only.

Both use shouldComponentUpdate to shallowly detect changes, so there's no practical difference between them, as long as a component doesn't involve local state.

like image 139
Estus Flask Avatar answered Sep 21 '22 04:09

Estus Flask