An open API service indexing awesome lists of open source software.

https://github.com/welingtonms/use-slice

Provides a React hook similar to the Redux Toolkit createSlice.
https://github.com/welingtonms/use-slice

dispatch react react-hooks reducers redux

Last synced: 7 months ago
JSON representation

Provides a React hook similar to the Redux Toolkit createSlice.

Awesome Lists containing this project

README

          

# use-slice

[![Coverage Status](https://img.shields.io/coveralls/github/welingtonms/use-slice?style=flat-square)](https://coveralls.io/github/welingtonms/use-slice)
[![npm package](https://img.shields.io/npm/v/@welingtonms/use-slice?style=flat-square)](https://www.npmjs.com/package/@welingtonms/use-slice)

Provides a React hook similar to the Redux Toolkit [createSlice](https://redux-toolkit.js.org/usage/usage-with-typescript#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](https://reactjs.org/docs/hooks-reference.html#usereducer).

## Instalation

You can add this hook as a dependency by running `npm install @welingtonms/use-slice` or `yarn add @welingtonms/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 your **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](https://codesandbox.io/embed/relaxed-fast-h4lv4?fontsize=14&hidenavigation=1&theme=dark).

```jsx
import React from 'react';
import { useSlice } from '@welingtonms/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 (


ToDoWithSlice




Description



Add



    { toDos.map( ( toDo, index ) => {
    return (

  • { toDo.description }

    Done


  • );
    } ) }


);
}

export default function App() {
return (



How to use @welingtonms/use-slice




);
}
```