Skip to content

Instantly share code, notes, and snippets.

@bibiboot
Created November 2, 2016 04:02
Show Gist options
  • Save bibiboot/9524baba27d87e48c2602a37b646814f to your computer and use it in GitHub Desktop.
Save bibiboot/9524baba27d87e48c2602a37b646814f to your computer and use it in GitHub Desktop.
Redux 2
const todo = (state = {}, action) => {
switch (action.type) {
case 'ADD_TODO':
return {
id: action.id,
text: action.text,
completed: false
};
case 'TOGGLE_TODO':
if (state.id !== action.id) {
return state;
}
return {
id: state.id,
text: state.text,
completed: !state.completed
};
default:
return state
}
};
const todos = (state = [], action) => {
switch (action.type) {
case 'ADD_TODO':
return [
...state,
todo(undefined, action)
];
case 'TOGGLE_TODO':
return state.map(t => todo(t, action));
default:
return state;
}
};
const { createStore } = Redux;
const store = createStore(todos);
store.dispatch({
type: 'ADD_TODO',
id: 0,
text: 'Go shopping'
});
store.dispatch({
type: 'ADD_TODO',
id: 1,
text: 'Go shopping'
});
store.dispatch({
type: 'TOGGLE_TODO',
id: 1
});
console.log(store.getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment