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

use-state-dispatch

v0.6.0

Published

A helper React hook for state and reducers

Downloads

4

Readme

useStateDispatch

Essentially a replacement for useReducer that offers better ergonomics.

Usage

npm install use-state-dispatch

const initialState = 5

const reducers = {
  increment(state: number) {
    return state+1
  },

  decrement(state: number) {
    return state -1
  }
}

const [state, dispatch] = useStateDispatch(initialState, reducers)

dispatch.increment()
dispatch.decrement()

Why it's nicer than useReducer

This is a nice alternative to useReducer because it gives you back a stable object with typed reducers.

I.e. dispatch.inc will autocomplete in Typescript to dispatch.increment.

This also lets you pass a stable refence to a function in e.g. an event handler to prevent unnecessary rerenders

const [state, dispatch] = useStateDispatch(initialState, reducers)

return (
  <button
    onClick={dispatch.increment}
  >
  </button>
)

In spirit, this is similar to the useEvent RFC in that dispatch functions are stable references to reducer functions with access to the latest state.

Return value

The return value of a dispatcher is a boolean notifying if a state update actually happened or not: if the returned state from a reducer is "shallow equal" to the current state, then no state update will happen, and the dispatcher will return false. Otherwise, it will return true. The notion of shallow equal does an Object.is check, unless its an array or object, in which case it performs the Object.is check on its elements (so it's not recursive and only updates one-level deep). This is same check that React uses.

const reducers = {
  tryIncrement(state: number) {
    if(state >= 1) return state
    return state+1
  },
}

const [state, dispatch] = useStateDispatch(0, reducers)

// Returns true, state is updated to 1
dispatch.increment()
// Returns false, state stays at 0
dispatch.increment()

This is nice if you're in a chaining flow for reducers: try reducers in sequence until one succeeds/is valid, like for keyboard shortcuts. Reducers often also have the reponsibility to validate a state update, so this can end up being useful.

The reducer form

The reducer form also has less boilerplate and is more straightforward:

// useReducer
function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return {count: state.count + 1};
    case 'decrement':
      return {count: state.count - 1};
    default:
      throw new Error();
  }
}

// useStateDispatch
const reducers = {
  increment(state: number) {
    return state+1
  },
  decrement(state: number) {
    return state -1
  }
}

In the useStateDispatch reducers, the first argument always needs to be the state. And any other arguments to a reducer function will need to be provided when dispatching.

const reducers = {
  increment(state: number, by: number) {
    return state+by
  },
}
...
const [state, dispatch] = useStateDispatch(initialState, reducers)

dispatch.increment(10)
dispatch.increment(20)

Typing the reducers object

Unfortunately, with Typescript, you can't place a type annotation hint on the reducers without collapsing its type.

This used to be the case, but with satisfies in Typescript 4.9, you can do this:

const reducers = {
  increment(state: number, by: number) {
    return state+by
  },
} satisfies AnyReducers<number>

const [state, dispatch] = useStateDispatch(initialState, reducers)

// Reducer functions and arguments still autocomplete
dispatch

If you can't use TS 4.9, you can use the AssertReducers type helper instead that asserts a type without collapsing it:

const reducers= {
  increment(state: number, by: number) {
    return state+by
  },
}

type ReducerTest = AssertReducers<number, typeof reducers>

The first generic type is the type of the state.