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

@cheesebit/use-slice

v0.1.2

Published

Provides a React hook similar to the Redux Toolkit createSlice.

Downloads

13

Readme

@cheesebit/use-slice

Provides a React hook similar to the Redux Toolkit createSlice.

Whenever you intend to use React's useReducer hook, useSlice can be a big help; it wires the whole reducers/actions parts for you out of the box so you can focus on the business logic of your app.

The idea is that you are able to create your reducers and actions to be used with React's useReducer.

Instalation

You can add this hook as a dependency by running npm install @cheesebit/use-slice or yarn add @cheesebit/use-slice.

Props

- name

Name of your slice. This is used to internally compose your actions' names (use this is case you want to dispatch your actions manually).

- initialState

Initial state for your slice.

- reducers

Finally, here you provide your reducers. Here we expect an object with your reducer functions. Each reducer function receives as parameters (as any reducer would), the current state and the action object. Important to notice that we handle the payload to you as an array, containing the arguments provided when your action was called (so your action can use as many parameter as needed).

What you get

When you call useSlice we generate a wrapper around your reducers, generating actions types and using React's useReducer to produce your "slice" of state, and return the following:

  • state - This is state that's being handled in your slice.

  • actions - This object holds you dispatchable actions ([!] do NOT forget to dispatch your action call), generated from the reducers you provided. That means, if your reducers object has a reducer increment, for instance, your actions object will have a function with the same name, so you can call it to perform the intended operation on your state.

  • dispatch - The dispatcher function.

Usage guide

Check a more complete example here.

import React from 'react';
import { useSlice } from '@cheesebit/use-slice';

function ToDoWithSlice() {
  const [description, setDescription] = React.useState('');
  const { state: toDos, actions, dispatch } = useSlice('todo', [], {
    addTodo(state, action) {
      const { payload } = action;
      const [newTodo] = payload;

      return [...state, newTodo];
    },
    setDone(state, action) {
      const { payload } = action;
      const [index] = payload;

      return [
        ...state.slice(0, index),
        {
          ...state[index],
          done: true,
        },
        ...state.slice(index + 1),
      ];
    },
  });

  return (
    <div className="block p-4">
      <h1>ToDoWithSlice</h1>
      <form className="flex flex-row items-end space-x-2 mb-4">
        <label className="flex flex-col items-start">
          Description
          <input
            type="text"
            className="border px-4 py-2"
            value={description}
            onChange={function handler(e) {
              const {
                target: { value },
              } = e;

              setDescription(value);
            }}
          />
        </label>
        <button
          type="button"
          className="bg-blue-500 px-4 py-2 text-white"
          onClick={function addTodo() {
            dispatch(
              actions.addTodo({
                description,
                done: false,
              })
            );

            // You could also write:
            // dispatch({
            //   type: actions.addTodo.type,
            //   payload: [{
            //     description,
            //     done: false
            //   }]
            // })

            setDescription('');
          }}
        >
          Add
        </button>
      </form>
      <ul className="list-disc flex flex-col items-stretch">
        {toDos.map((toDo, index) => {
          return (
            <li
              key={index}
              className={`flex items-center border-b py-2 ${toDo.done && 'line-through'}`}
            >
              {toDo.description}
              <button
                type="button"
                disabled={toDo.done}
                className="bg-green-500 px-4 py-2 text-white ml-auto mr-0"
                onClick={function addTodo() {
                  dispatch(actions.setDone(index));
                  setDescription('');
                }}
              >
                Done
              </button>
            </li>
          );
        })}
      </ul>
    </div>
  );
}

export default function App() {
  return (
    <div className="App">
      <h1>How to use @cheesebit/use-slice</h1>
      <ToDoWithSlice />
    </div>
  );
}