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

compose-state

v1.0.13

Published

Compose multiple setState or getDerivedStateFromProps updaters in React

Downloads

5

Readme

✍️ compose-state

compose-state is a library that composes multiple state updaters in React.

compose-state works with the standard setState parameters – objects or functions – so you don’t have to learn any new syntax. It’s also compatible with React’s new getDerivedStateFromProps lifecycle method.

Use

Install

yarn add compose-state or npm install compose-state

Import

import composeState from 'compose-state';

// or

import {
  composeState,
  composeDerivedStateFromProps,
} from 'compose-state';

API

composeState([updaters])

Returns an updater that can be used with setState. Calling this produces the same result as calling or accessing each given updater from right to left, merging each partial state. If a given updater is a function, its prevState value is the previous state merged with the current partial state.

Arguments

| Name | Type | Description | | - | - | - | | [updaters] | (...Updater) | Functions that can be used with setState, or partial state objects |

composeDerivedStateFromProps([updaters])

Returns an updater that can be set as a component's getDerivedStateFromProps static value. Calling this produces the same result as calling or accessing each given updater from right to left, merging each partial state. If a given updater is a function, its prevState value is the previous state merged with the current partial state.

Arguments

| Name | Type | Description | | - | - | - | | [updaters] | (...Updater) | Functions that can be set as a component's getDerivedStateFromProps static value, or partial state objects |

Benefits

Simplify your updaters and React code

Let's say you want to call setState and do two things

  1. Increment a score value by 1
  2. Log the current time to an array

Both of these updaters need to be functional, since they rely on the previous state for their return values.

const updateScore = s => ({ score: s.score + 1 });
const logTime = s => ({ log: [...s.log, Date.now()] });

Normally, we would need to call setState for both of these functions

class Game extends Component {
  onScore = () => {
    this.setState(updateScore);
    this.setState(logTime);
  };
  // ...
}

...or we rewrite the two updaters into one larger function.

const updateScoreLogTime = s => ({
  score: s.score + 1,
  log: [...s.log, Date.now()],
});

But with compose-state, we can keep these two updaters independent, and we won't have to bulk up our component code with more setState calls.

const updateScoreLogTime = composeState(updateScore, logTime);

class Game extends Component {
  onScore = () => {
    this.setState(updateScoreLogTime);
  };
  // ...
}

compose-state isn't dependent on React at all, it's just a big reducer function. This allows you to build and compose as many updaters as you want while keeping your actual component code simple and maintainable.

Easily mix functional and object updaters

compose-state accepts both objects and functions, just like setState. This allows you to mix static and dynamic updaters without increasing the complexity of any individual parameter.

const defaultValue = { value: 0 };
const incrementOther = s => ({ other: s.other + 1 });

this.setState(
  composeState(defaultValue, incrementOther)
);

Compatibility with getDerivedStateFromProps

compose-state comes with a composeDerivedStateFromProps function to use with React's new getDerivedStateFromProps lifecycle method.

const updater1 = (nextProps, prevState) => {
  // ...
}
const updater2 = (nextProps, prevState) => {
  // ...
}

class App extends Component {
  static getDerivedStateFromProps = composeDerivedStateFromProps(
    updater1, updater2
  )
  // ...
}

It's just normal, boring React

While more formal state managers push developers away from controlling state in React, compose-state simply enhances state control methods that are primitive to the platform.

compose-state is a lot like Classnames. It's a helper function that makes setState calls more declarative and easier to construct, just like how Classnames is a helper function that makes className values more declarative and easier to construct.

Further reading

Functional setState is the future of React by Justice Mba

Best kept React secret: you can declare state changes separately from the component classes. by Dan Abramov