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

react-verbal-reducer

v1.0.9

Published

An extension of React's 'useReducer' hook to automatically type and map reducer actions.

Downloads

5

Readme

React Verbal Reducer

Verbal reducers are extensions of the React useReducer hook. A verbal reducer automatically applies types to actions, and provides a mapped version of each action creator directly to the component.

This serves as an alternative to dispatch, and is especially useful within context providers.

Install

npm i --save react-verbal-reducer

Simple Example

import { verbalReducer } from 'react-verbal-reducer'

const initialState = { count: 0 }

const reducer = verbalReducer(initialState)(
  {
    set: (count: number) => ({
      count,
    }),
    increment: {},
    decrement: {},
  },

  (state, action) => {
    switch (action.type) {
      case 'set':
        return { count: action.count }
      case 'increment':
        return { count: state.count + 1 }
      case 'decrement':
        return { count: state.count - 1 }
    }
  },
)

function Counter() {
  const [state, actions] = reducer.use()
  return (
    <>
      Count: {state.count}
      <button onClick={() => actions.set(state.count + 50)}>+50</button>
      <button onClick={actions.increment}>+</button>
      <button onClick={actions.decrement}>-</button>
    </>
  )
}

Usage

The core purpose of verbal reducers is to provide mapped actions directly to the component in place of React dispatch. Just like dispatch, actions are guarenteed have a stable function identity.

The Reducer

The reducers, actions and (optional) default state are all defined by means of the verbalReducer function.

Unlike regular reducers, the update object returned verbal reducers gets merged as oposed to set (similar to the class component setState method).

const reducer = verbalReducer({ username: '', count: 0 })(
  {
    setUsername: (username: string) => ({
      username,
    }),
  },
  (state, action) => {
    switch (action.type) {
      case 'setUsername':
        // results in { username: action.username, count: 0 }
        return { username: action.username }
    }
  },
)

Typed State

In the above example, the Typescript compiler will interpret the state type directly from the default definition. This could be problematic for initial state values derived from props. So, you can also apply a state type directly.

interface State {
  username: string
  count: number
}

const reducer = verbalReducer<State>({ username: 'guest' })(
  ...
)

You can then supply default state values within the component:

function Counter(props) {
  const [state, { setUsername, setCount }] = reducer.use({
    count: props.count,
  })
}

Note: The above example also demonstrates how to deconstruct the actions and access them directly. This is usefull when you don't have a need to use the entire actions object itself.