Skip to content

Yet another lib for creating typed actions and reducers.

License

Notifications You must be signed in to change notification settings

Cooke/redux-ts-simple

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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();

About

Yet another lib for creating typed actions and reducers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •