Skip to content

Purify your actions and reducers, no more boilerplate and purified sin free code for ya

Notifications You must be signed in to change notification settings

alpacaaa/redux-purify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What if you could define reducers and actions all at once?

import purify from 'redux-purify';

const { actions, reducer } = purify({
  increment(state, action) => ({
    ...state,
    counter: state.counter + 1,
  }),


  decrement(state, action) => ({
    ...state,
    counter: state.counter - 1,
  }),


  setCounter(state, action, value) => ({
    ...state,
    counter: value,
  })
});


const store = createStore(reducer);

store.dispatch(actions.increment());
store.dispatch(actions.decrement());

store.dispatch(actions.setCounter(5));

If you pass along a second parameter, you get an initialState.

const initialState = {
  sandwichesMade: 0,
};

const { actions, reducer } = purify({
  makeASandwich(state, action) => ({
    ...state,
    sandwichesMade: state.sandwichesMade + 1,
  }),
}, initialState);


const store = createStore(reducer);

store.dispatch(actions.makeASandwich());
store.dispatch(actions.makeASandwich());

console.log(store.getState());
// { sandwichesMade: 2 }

Note to self: this might not be the worst idea ever.

About

Purify your actions and reducers, no more boilerplate and purified sin free code for ya

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published