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-reducer-actions

v1.1.0

Published

A react hook that wraps useReducer and provides an object-based shorthand inspired by redux toolkit

Downloads

10

Readme

useReducerActions

A react hook that wraps useReducer and provides an object-based shorthand inspired by the createSlice() api in redux toolkit

Installation

Install to your project via yarn add use-reducer-actions or npm install use-reducer-actions

Usage

Utilize the explicit state behavior of useReducer with a typesafe object syntax

First, define your state and initial state

type State = {
  loading: boolean,
  values: string[]
}

let initialState: State = {
  loading: false,
  values: []
}

Next, define your reducer object, and object with keys as "action titles" and values as "reducer actions". Essentially, you describe what the generated reducer will do (the reducer action) when the specified action (the action title) case is passed.

import { PayloadAction } from "use-reducer-actions";

const reducerObject = {
  reset: () => {
    return initialState;
  },
  append: (state: State, action: PayloadAction<string>) => {
    return {
      ...state,
      values: state.values.concat(action.payload)
    };
  },
  pop: (state: State) => {
    let newValues = state.values.slice(0, state.values.length - 1);
    return {
      ...state,
      values: newValues
    };
  }
}

Now you can use your stateful logic as easily invoked actions - no need to call a dispatch function. All together now:

import React from "react";
import { useReducerActions, PayloadAction } from "use-reducer-actions";

type State = {
  loading: boolean,
  values: string[]
}

let initialState: State = {
  loading: false,
  values: []
}

const reducerObject = {
  reset: () => {
    return initialState;
  },
  append: (state: State, action: PayloadAction<string>) => {
    return {
      ...state,
      values: state.values.concat(action.payload)
    };
  },
  pop: (state: State) => {
    let newValues = state.values.slice(0, state.values.length - 1);
    return {
      ...state,
      values: newValues
    };
  }
}

const MyComponent: React.FC = () => {
  const [state, actions] = useReducerActions(reducerObject, initialState);

  return (
    <div>
      My values: {state.values.join(", ")}
    </div>
    <div>
      <button onClick={() => actions.append(`value ${state.values.length}`)}>
        Append Value
      </button>
      <button onClick={() => actions.pop()}>
        Pop Value
      </button>
      <button onClick={() => actions.reset()}>
        Reset
      </button>
    </div>
  )
}

Tips

To avoid referential equality issues (which may cause unnecessary rerenders), it is recommended to define your reducer object outside your component. If your reducer object is dependent on state or props and you must define it in your component, consider wrapping it in useMemo.

To allow typescript to fully infer your actions, do not annotate your reducer object with a type.

// this is bad; it will prevent accurate type inference
const reducer: Record<string, (state: S, action: A) => void> = {
  reset: () => initialState,
  append: (state, action) => {
    return { ...state, values: state.values.concat(action.payload.value) };
  }
}

Instead, annotate the reducer action functions inside. Typescript will then infer action names and typings.

// this is what we want
const reducer = {
  reset: () => initialState,
  append: (state: S, action: PayloadAction<{ value: string }>) => {
    return { ...state, values: state.values.concat(action.payload.value) };
  }
}

You may have to pass generics to useReducerActions to help out when state is very tightly defined. Using typeof {your reducer object} can do most of the heavy lifting when it comes to type inference.

const [state, actions] = useReducerActions<State, typeof reducerObject>(reducerObject, initialState);

Future Work

It could be nice to include Immer in the same way that redux-toolkit does, but as a feature this is fairly low priority for me.

Typings can get out of sync quite quickly. And when the reducer object is defined outside of its usage in the component, the errors can be hard to read. It may be useful to define a "higher-order hook" - a function that you call outside of a component that returns a hook that is bound to you reducerObject and initialState, much like the original createSlice() api does. That way at the very least type inference may be more intuitive, no need to use typeof {your reducer object} in a generic.

Limitations

While the typings are complete, there are two uses of any in the implementation of the declarations (meaning the javascript can't quite link up the typings). While one of the instances is definitely due to the complexities of "mapping" an object via its keys in javascript, the other I'm less sure of. Hopefully these can evolve out, but it is possible there are small bugs due to these typing issues.

This project was bootstrapped with TSDX.