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

@uplab/should-update

v1.2.1

Published

shouldComponentUpdate without headache 🤕

Downloads

11

Readme

Install

npm i --save @uplab/should-update

Intro

There is a time when a React Developer evolves to a Good React developer and decides to add shouldComponentUpdate to every component that cannot be PureComponent. In most cases ones shouldComponentUpdate function implementation looks similar to this:

class DeathStar extends Component {
  shouldComponentUpdate(nextProps) {
    return (
      this.props.jedi.id !== nextProps.jedi.id ||
      this.props.jedi.name !== nextProps.jedi.name ||
      this.props.jedi.profile.firstName !== nextProps.jedi.profile.lastName
      // ... forevermore 
    );
  }
}

Solution

This package is made to simplify the API for props and state comparison to a single function call. @uplab/should-update can deal with nested properties and accepts an array on dependencies that are presented as a path string (i.e. some.very.deeply.nested.data.and.even.array[0].items). It can track not only the props change, but a state changes.

Example usage:

import { createShouldUpdate } from '@uplab/should-update';

class DeathStar extends Component {
  state = {
    formState: {
      firstName: 'Anakin',
      lastName: 'Skywalker',
    }
  }
  shouldComponentUpdate: createShouldUpdate({
    dependencies: ['jedi.id', 'jedi.name', 'jedi.profile.firstName'],
    stateDependencies: ['formState'],
  })
}

or

import { shouldUpdate } from '@uplab/should-update';

class DeathStar extends Component {
  state = {
    formState: {
      firstName: 'Anakin',
      lastName: 'Skywalker',
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const dataChanged = shouldUpdate({
      dependencies: ['jedi.id', 'jedi.name', 'jedi.profile.firstName'],
      stateDependencies: ['formState'],
      state: this.state,
      props: this.props,
      nextProps,
      nextState,
    });
    // some custom stuff with dataChanged
  }
}

By default, if the resolved path is a type of object, then the deep comparison happens to check all the nested props. To avoid this behavior when you have complex and huge objects you can pass shallow: true prop:

shouldComponentUpdate: createShouldUpdate({ dependencies: ['jedi.profile'], shallow: true, stateDependencies: ['formState'] })

or

shouldUpdate({
  dependencies: ['jedi.profile'],
  props: this.props,
  nextProps,
  shallow: true, //defaults to false
  stateDependencies: ['formState'],
  state: this.state,
  nextState,
});

API

shouldUpdate(params)

  • @param {array} params.dependencies - array of pathes of the properties to depend on
  • @param {object} params.props - component props
  • @param {object} params.nextProps - component changed props. Can be previous or next props
  • @param {boolean} [params.shallow = false] - if true then the function will do shallow comparison.
  • @param {array} params.stateDependencies - array of pathes of the properties to depend on
  • @param {object} params.state - component state
  • @param {object} params.nextState - component changed state. Can be previous or next state

Returns true if the component should update, else false.

createShouldUpdate(params)

Same as shouldUpdate, but without params.props and params.nextProps