Last active
February 21, 2016 04:19
-
-
Save codemilli/fe181d48501285a17771 to your computer and use it in GitHub Desktop.
simple reducers
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 const visible = (state = 'SHOW_ALL', action) => { | |
switch (action.type) { | |
case 'SET_VISIBLE': | |
return action.filter; | |
default: | |
return state; | |
} | |
}; | |
const toggleTodo = (todo, id) => { | |
if (todo.id !== id) { | |
return todo; | |
} | |
return Object.assign({}, todo, { | |
completed: !todo.completed | |
}); | |
}; | |
export const todos = (state = [], action) => { | |
switch (action.type) { | |
case 'CREATE_TODO': | |
return [ | |
...state, | |
{ | |
id: action.id, | |
text: action.text, | |
completed: false | |
} | |
]; | |
case 'TOGGLE_TODO': | |
return state.map(t => toggleTodo(t, action.id)); | |
default: | |
return state; | |
} | |
}; | |
import { combineReducers } from 'redux'; | |
export default combineReducers({ visible, todos }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment