-
-
Save coder-layne/1decb3550b3b3addd601230c9e4ec5d7 to your computer and use it in GitHub Desktop.
simplified redux
This file contains hidden or 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