Skip to content

Instantly share code, notes, and snippets.

@rmoorman
Forked from kitze/store.js
Created September 10, 2018 15:46
Show Gist options
  • Select an option

  • Save rmoorman/5fe0f93c49a27f807c9e5cced202b08d to your computer and use it in GitHub Desktop.

Select an option

Save rmoorman/5fe0f93c49a27f807c9e5cced202b08d 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment