Skip to content

Instantly share code, notes, and snippets.

@nhagen
Created October 25, 2015 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhagen/e5e2f6d7c63f96c7a082 to your computer and use it in GitHub Desktop.
Save nhagen/e5e2f6d7c63f96c7a082 to your computer and use it in GitHub Desktop.
Export reducer instead of switch
export default function(initialState, handlers) {
return function(state = initialState, action) {
const handler = handlers[action.type];
return typeof hander === 'function' ? handler(state, action) : state;
}
}
import * as todoHandlers from 'todos';
import createReducer from 'createReducer';
import { createStore } from 'redux';
export default createStore(createReducer({}, todoHandlers));
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