Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use React.PureComponent everywhere?

React says pure render can optimize performance. And now React has PureComponent. Should I use React.PureComponent everywhere? Or when to use React.PureComponent and where is the most proper postion to use React.PureComponent?

like image 951
Lxxyx Avatar asked Mar 13 '17 03:03

Lxxyx


People also ask

When should we use React PureComponent?

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. PureComponent for a performance boost in some cases.

Should I update PureComponent component?

React. PureComponent implements shouldComponentUpdate() method to determine re-rendering with a shallow prop and state comparison for rendering performance optimization. Although overridden shouldComponentUpdate() method will work as intended, it is recommended to extend React.

Should all components be pure?

A component must be pure, meaning: It minds its own business. It should not change any objects or variables that existed before rendering. Same inputs, same output.

Should we use pure component in React?

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


2 Answers

Not always. You should use it when a component could re-render even if it had the same props and state. An example of this is when a parent component had to re-render but the child component props and state didn't change. The child component could benefit from PureComponent because it really didn't need to re-render.

You shouldn't necessarily use it everywhere. It doesn't make sense for every component to implement the shallow shouldComponentUpdate(). In many cases it adds the extra lifecycle method without getting you any wins.

It can drastically save you rendering time if you have many cases where root level components update while the children do not need to do so. That being said, you will gain much more from using PureComponent in your root level nodes (i.e. PureComponent will not improve overall performance as much in leaf level components because it only saves renders from that component).

Some caveats to PureComponent are explained very well in the react docs.

React.PureComponent's shouldComponentUpdate() only shallowly compares the objects.

You could accidentally miss rendering updates if your prop changes are deep in a nested object. PureComponent is only great with simple flat objects/props or by using something like ImmutableJS to detect changes in any object with a simple comparison.

Furthermore, React.PureComponent's shouldComponentUpdate() skips prop updates for the whole component subtree. Make sure all the children components are also "pure".

PureComponent only works whenever the rendering of your component depends only on props and state. This should always be the case in react but there are some examples where you need to re-render outside of the normal react lifecycle. In order for PureComponent to work as expected (skipping the re-rendering you don't really need to happen), every descendant of your PureComponent should be 'pure' (dependent only upon props and state).

like image 121
Special Character Avatar answered Oct 12 '22 10:10

Special Character


According to this reference, no (you should not use it everywhere):

(I work on React.) [...] If we recommended that PureComponent be used everywhere, it would probably be the default already. Rather -- the comparison to decide whether or not a component should be rerendered costs something, and in the case that you do want to rerender, all of the time spent checking whether you should have rerendered is wasted.

Instead, we'd suggest you be conscious of where you need to do the comparisons. It's usually only in a couple of places in your app. Good candidates are on the children of a long list or around large parts of the app that change independently (that is, cases where you know the parent should often rerender but the child shouldn't). A few well-placed shouldComponentUpdate (or PureComponent) uses can go a long way.

--

I found a tweet from Dan Abramov saying the same thing (30 Jul 2016).

like image 45
ghashi Avatar answered Oct 12 '22 10:10

ghashi