1.0.1 • Published 3 years ago

redux-slice v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

redux-slice - utility library to help reduce boilerplate code in Shipa Redux-based Separation of Concerns implementation (SReSC).

Use

  1. Install.
npm i @shipae/redux-slice
  1. Use in code to auto generate reducer and actions for application module.
import { createSlice, Action } from 'src/application/slice-util';

type CounterPayload = number | undefined;
export type CounterAction = Action<CounterPayload>;
export type CounterState = {
  value: number,
};

export const {
  reducer,
  actions: {
    incrementCounter,
    decrementCounter,
    updateCounter,
  },
} = createSlice<CounterState, CounterPayload>(
  { value: 0 },
  [
    { type: 'incrementCounter' },
    { type: 'decrementCounter' },
    { type: 'updateCounter', field: 'value' },
  ],
);

Concept

In SReSC all logic sits in redux middlewares so nor reducer neither action should newer contain any logic which means both turn almost completely into boilerplate code. This also means that any action creator can have the same shape, for example:

updateCounter = (payload) => ({
  type: 'updateCounter',
  payload,
});

Its just a payload that changes but the action itself and action creator are always the same. Payload can be anything that is why lib also exports Action type which has typescript generic Payload:

type Action<Payload> = {
  type: string,
  payload: Payload,
};

Improve project

  1. npm i - install dependencies
  2. npm run dev - start typescript type checker
  3. (optional) npm run jest - start jest in watch mode