Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
simplified redux
import produce from 'immer';
import {createStore} from 'redux';
const handleActions = (actionsMap, defaultState) => (
state = defaultState,
{type, payload}
) =>
produce(state, draft => {
const action = actionsMap[type];
action && action(draft, payload);
});
const todosReducer = handleActions(
{
ADD_TODO: (todos, todo) => todos.push(todo),
REMOVE_TODO: (todos, id) => todos.splice(todos.findIndex(t => t.id === id), 1),
COMPLETE_TODO: (todos, id) => todos[todos.findIndex(t => t.id === id)].complete = true,
UPDATE_TODO: (todos, {id, data}) => todos[todos.findIndex(t => t.id === id)] = data
},
[]
);
const store = createStore(todosReducer);
@drewhamlett

This comment has been minimized.

Copy link

commented Jan 28, 2018

I did something like this so I wouldn't have param reassign eslint issues.

const handleActions = (actionsMap, defaultState) => (
  state = defaultState,
  { type, ...params },
) =>
  produce(state, draft => {
    const action = actionsMap[type]
    return action && action.call(draft, params)
  })


export default handleActions(
  {
    BOOTING() {
      this.booting = true
    },
    BOOTING_COMPLETE(action) {
      this.booting = false
      this.user = action.user
      this.isLoggedIn = action.isLoggedIn
    },
  },
 {},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.