Pure render checks
In order to preserve performance one needs to consider the creation of new entities in the render method.
Pure render?
With React.js pure render I am talking about components that implement the shouldComponentUpdate
method with shallow equality checks.
Examples of this are the React.PureComponent
, PureRenderMixin
, recompose/pure and many others.
Case 1
The bad way
The issue is with {this.props.options || []}
- it cause all the cells to be re-rendered even for a single cell change. Why?
You see the options array was passed deep down in the Cell elements. Normally this would not be an issue. The other Cell elements would not be re-rendered because they can do the cheap shallow equality check and skip the render entirely but in this case the options prop was null and the default array was used.
As you should know the array literal is the same as new Array() which creates a new array instance. This completely destroyed every pure render optimization inside the Cell elements. In Javascript different instances have different identities and thus the shallow equality check always produces false and tells React to re-render the components.
The good way
Case 2
Similar issue with using functions in render() as well
The bad way
Another bad way
In both above cases, a new function is created with a new identity. Just like with the array literal. We need to bind the function early.
The good way
The bad way
The good way
Related links:
Last updated
Was this helpful?