Skip to content

Instantly share code, notes, and snippets.

@kitze
Created January 24, 2018 13:14
Show Gist options
  • Save kitze/fb65f527803a93fb2803ce79a792fff8 to your computer and use it in GitHub Desktop.
Save kitze/fb65f527803a93fb2803ce79a792fff8 to your computer and use it in GitHub Desktop.
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
Copy link

drewhamlett 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