Pure Render Checks

    Pure render?

    Examples of this are the React.PureComponent, PureRenderMixin, recompose/pure and many others.

    Case 1

    Bad

    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.

    Good
    1. const defaultval = []; // <--- The fix (defaultProps could also have been used).
    2. class Table extends PureComponent {
    3. render() {
    4. return (
    5. <div>
    6. {this.props.items.map(i =>
    7. <Cell data={i} options={this.props.options || defaultval}/>
    8. </div>
    9. );
    10. }
    11. }

    Case 2

    BAD
    Bad again
    1. class App extends PureComponent {
    2. this.props.update(e.target.value);
    3. }
    4. render() {
    5. return <MyInput onChange={this.update.bind(this)}/>;
    6. }
    7. }

    ^^In both cases a new function is created with a new identity. Just like with the array literal.
    We need to bind the function early

    Good
    Bad
    1. class Component extends React.Component {
    2. state = {clicked: false};
    3. onClick() {
    4. }
    5. render() {
    6. // Options object created each render if not set
    7. const options = this.props.options || {test: 1};
    8. return <Something
    9. options={options}
    10. // New function created each render
    11. onClick={this.onClick.bind(this)}
    12. // New function & closure created each render
    13. onTouchTap={(event) => this.onClick(event)
    14. />
    15. }
    16. }
    Good
    • @esamatti/react-js-pure-render-performance-anti-pattern-fb88c101332f"">https://medium.com/@esamatti/react-js-pure-render-performance-anti-pattern-fb88c101332f