npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shouldcomponentupdate-children

v1.1.0

Published

'Shallow Equal' HOC implementation to optimize shouldComponentUpdate with children / React elements.

Downloads

1,805

Readme

shouldComponentUpdate-Children

A PureComponent alternative that will actually improve your application performance!

"Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements.

See live example here: codepen.io/NoamELB/pen/RLoxLv

Usage

Install

npm i -S shouldcomponentupdate-children

Option 1: As an HOC when exporting a component:

import {useShallowEqual} from 'shouldcomponentupdate-children';

class MyComponent extends React.Component {
    ....
}
const MyPerformantComponent = useShallowEqual(MyComponent);

export default MyPerformantComponent;

Option 2: As an HOC when importing a component:

import {useShallowEqual} from 'shouldcomponentupdate-children';
import MyComponent from './my-component';

const MyPerformantComponent = useShallowEqual(MyComponent); // use it just like you would use MyComponent

Option 3: As the shouldComponentUpdate implementation

import {shallowEqual} from 'shouldcomponentupdate-children';

class MyComponent extends React.Component {
    shouldComponentUpdate(nextProps, nextState) {
        return shallowEqual(this.props, nextProps, this.state, nextState);
    }
}
export default MyComponent;

The Problem

React will create a new instance of a React Element on each render, so generic implementations to shouldComponentUpdate will return true even if nothing had changed!

Basically, this simple shouldComponentUpdate implementation:

return this.props.children !== nextProps.children;

is almost as good as writing:

return true;

See live example here: codepen.io/NoamELB/pen/RLoxLv

Read more about it here: https://medium.com/myheritage-engineering/how-to-greatly-improve-your-react-app-performance-e70f7cbbb5f6

Our Solution

We created an HOC which uses Inheritance Inversion to extend components with a generic shouldComponentUpdate functionality.

Our generic shouldComponentUpdate implementation does the following:

  • execute the wrapped component's shouldComponentUpdate and continue only if returned true.
  • shallow equal this.state vs next state and return true if not equal.
  • shallow equal all this.props vs next props, but skip React Elements. return true if not equal.
  • if reached here - returns false

But isn't this means that if any React Element is actually changing then my component won't render?

Yes, but that is the whole point. React Elements are not something you can rely upon when implementing shouldComponentUpdate!

In order to tell a component that it should render - you can change any non-React-Element prop to indicate a state change (this can be a designated prop just for that or a prop that is actually in use inside the component).

Q&A

Why use shouldComponentUpdate?

Performance. If the change in state or props does not affect your component, you can tell React. React will not re-render your component for no reason in that case. Read more: https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate

How to use shouldComponentUpdate?

MyClass extends React.Component {
    ...
    shouldComponentUpdate(nextProps, nextState) {
       // return true or false
    }
}

It's common to want to re-render when either the state or any prop has changed - so react created a class that does it for us:

MyClass extends React.PureComponent {
    ...
    // don't need to implement shouldComponentUpdate
}

Read more: https://facebook.github.io/react/docs/react-api.html#react.purecomponent

What's the problem with PureComponents?

If "children" (or any other prop) is a React element or an array, it will send a new instance every time. This means that in most cases, the following check will always be true:

this.props.children !== nextProps.children

The result is that our component will render even if our prop didn't actually change.

The solution - shouldComponentUpdate-Children

Wrap your components with this HOC and it will perform a wiser shallow equal check. It will skip props that would have always returned true, regardless of their value:

const MyPerformantComponent = useShallowEqual(MyComponent);

How do you determine that a prop is a React Element

  1. Any prop that returns true for React.isValidElement(prop).
  2. Any array prop that have at least one item which returns true for React.isValidElement(prop[i])

Why implement as an HOC?

HOC is very useful in this specific case of Inheritance Inversion, you can use it on the outside when importing. Let's see some nice example of when to use on the outside:

  • Using a vendor component that has performance issues? Just wrap it with the HOC after the import.
  • Implementing shouldComponentUpdate in a given component is a refactor headache? HOC to the rescue.
  • shouldComponentUpdate is already implemented but not good enough for your usage? I <3 HOC.

By tha way, we exports all of the functions from the package, so you can't use them directly when implementing shouldComponentUpdate:

  • shouldComponentUpdate(nextProps, nextState) - bind the "this" to the function and just use it as your shouldComponentUpdate.
  • shallowEqual(this.props, nextProps, this.state, nextState) - same but with no need to bind anything.
  • shallowEqualWithoutReactElements(thisProps, nextProps) - the actual shallow equal implementation on the props.
  • shallowEqualState(thisState, nextState) - the actual shallow equal on the state.

License

MIT