redux-slice
v1.0.1
Published
redux utility that generates actions and reducer automatically suitable for Shipa Redux-based Separation of Concerns approach.
Downloads
1
Readme
redux-slice - utility library to help reduce boilerplate code in Shipa Redux-based Separation of Concerns implementation (SReSC).
Use
- Install.
npm i @shipae/redux-slice
- 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
npm i
- install dependenciesnpm run dev
- start typescript type checker- (optional)
npm run jest
- start jest in watch mode