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-use-reducers

v0.1.1

Published

A simple, tiny, type-safe way to manage state, without any of the boilerplate of typical reducers.

Downloads

2

Readme

react-use-reducers

A simple, tiny, type-safe way to manage state, without any of the boilerplate of typical reducers.

Install via npm install react-use-reducers

Import via import { useReducers } from 'react-use-reducers';

Simple "Counter" Example

No better documentation than an example:

import { useReducers } from 'react-use-reducers';

const INITIAL = { count: 0 };
type TState = typeof INITIAL;
const REDUCERS = {
  increment: (state: TState, amount = 1) => ({ count: state.count + amount }),
  reset: (state: TState) => INITIAL,
};

function App() {
  const [ state, actions ] = useReducers(REDUCERS, INITIAL);
  // state === { count: 0 }
  // actions === { 
  //               increment(amount?: number): void, 
  //               reset(): void, 
  //             }

  return (
    <div>
      <h1>Count: {state.count}</h1>
      <button onClick={() => actions.increment()}> +1 </button>
      <button onClick={() => actions.increment(99)}> +99 </button>
      <button onClick={actions.reset}> Reset </button>
    </div>
  );
}

useReducers API

const [ state, actions ] = useReducers(reducers, initial);

Parameters

initial - the initial state. Can be a value or a callback that returns a value.

reducers - an object with named reducers. In the above example, there are 2 reducers (increment and reset).

Each reducer takes the previous state, and returns the new state.

Reducers can have additional parameters, like how increment has an optional amount parameter.

Returns

state - the current state

actions - an object with the same shape as the reducers (except without the state parameter)

Each action can be called (along with any additional parameters) to update the state.

For example, actions.reset() or actions.increment(99).

The actions object and methods will ALWAYS be the same reference, so it's safe to omit it from dependency lists. Example:

useEffect(() => {
  const t = setInterval(() => actions.increment(), 1000);
  return () => clearInterval(t);
}, []); // Empty dependency list, because `actions` doesn't change

Things to Note

  • Multiple actions can be called sequentially, and will be applied sequentially. So if you called actions.reset(); actions.increment(5) you'd end up with { count: 5 }
  • Reducers can be "inline" inside the component. They can access properties or other state, if needed. Example:
    function Counter(props) {
      const [ state, actions ] = useReducers({
        increment: (prev) => prev + props.size,
        reset: () => 0
      }, 0);
      // ... 
    }

Advanced "To Do App" Example

import { useReducers } from 'react-use-reducers';

const INITIAL = { todos: [] };
const REDUCERS = {
  addTodo(prev, title) {
    return ({
      todos: [
        ...s.todos,
        { title, complete: false }
      ]
    });
  },
  updateTodo(prev, title, complete) {
    return ({
      todos: prev.todos.map(todo => {
        if (todo.title === title) {
          return { ...todo, complete };
        }
        return todo;
      })
    });
  },
  removeTodo(prev, title) {
    return ({
      todos: prev.todos.filter(todo => todo.title !== title),
    });
  },
  reset: (prev) => INITIAL,
};

function TodoApp() {
  const [ state, actions ] = useReducers(REDUCERS, INITIAL);

  // state === {
  //             todos: [ ... ]
  //           }
  // typeof actions === { 
  //                      addTodo(title): void; 
  //                      updateTodo(title, complete): void;
  //                      removeTodo(title): void;  
  //                      reset(): void  
  //                    }

  return <>TODO</>;
}