Created
October 25, 2015 20:34
-
-
Save nhagen/e5e2f6d7c63f96c7a082 to your computer and use it in GitHub Desktop.
Export reducer instead of switch
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
export default function(initialState, handlers) { | |
return function(state = initialState, action) { | |
const handler = handlers[action.type]; | |
return typeof hander === 'function' ? handler(state, action) : state; | |
} | |
} |
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 * as todoHandlers from 'todos'; | |
import createReducer from 'createReducer'; | |
import { createStore } from 'redux'; | |
export default createStore(createReducer({}, todoHandlers)); |
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
export function MAKE_TODO(text, completed) { | |
return { | |
text: text, | |
completed: completed | |
}; | |
} | |
export function COMPLETE_TODO(todo) { | |
Object.assign({}, todo, { | |
completed: true | |
}); | |
} | |
export function RENAME_TODO(todo, text) { | |
Object.assign({}, todo, { | |
text: text | |
}); | |
} | |
export function MAKE_TODO() { | |
return []; | |
} | |
export function ADD_TODO(todos, text, completed) { | |
return [...todos, MAKE_TODO(text, completed)]; | |
} | |
export function MODIFY_TODO(todos, index, modify) { | |
return [ | |
...todos.slice(0, index), | |
modify(todos[index]), | |
...todos.slice(index + 1) | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment