Created
January 24, 2018 13:14
-
-
Save kitze/fb65f527803a93fb2803ce79a792fff8 to your computer and use it in GitHub Desktop.
simplified redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
I did something like this so I wouldn't have param reassign eslint issues.