Skip to content

Instantly share code, notes, and snippets.

@codemilli
Last active February 21, 2016 04:19
Show Gist options
  • Save codemilli/fe181d48501285a17771 to your computer and use it in GitHub Desktop.
Save codemilli/fe181d48501285a17771 to your computer and use it in GitHub Desktop.
simple reducers
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