3.2.0 • Published 5 years ago

redux-ts-simple v3.2.0

Weekly downloads
25
License
MIT
Repository
github
Last release
5 years ago

redux-ts-simple

Yet another lib for creating typed actions and reducers. This library is FSA-compliant.

Installing

npm install redux-ts-simple

Usage

action.ts

import { createAction } from "redux-ts-simple";

// Define action creators
const reset = createAction('Reset');
const increment = createAction<number>('Increment');
const set = createAction<{ value: number }>('Set');

// Usage
dispatch(reset());
dispatch(increment(100));
dispatch(set({ value: 333 }));

// Error and meta
const fetch = createAction<string | Error>('Fetch');
const fetchAction = fetch(new Error('Something went wrong'), { url: '/posts/1' });
console.log(fetchAction.error); // true
console.log(fetchAction.meta.url); // /posts/1

reducer.ts

import { ReducerBuilder } from "redux-ts-simple";

const initialState = { counter: 0, version: 0 };

const reducer = new ReducerBuilder(initialState)
    .on(reset,       () => ({ counter: 0 }))
    .on(increment,   (state, action) => ({ counter: state.counter + action.payload }))
    .on(set,         (state, action) => ({ counter: state.counter + action.payload.value }))
    .on(clear, wipe, (state) => ({ counter: 0}) // Several actions with same handler
    .else(           (state, action) => state) // Extensibility point 
    .every(          (state, action) => ({ version: state.version + 1}) // Executed after every action and else handler with the new state as input
    .build();
3.2.0

5 years ago

3.1.5

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.0.1

7 years ago

1.2.1

7 years ago

2.0.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago