Created
November 2, 2016 04:02
-
-
Save bibiboot/9524baba27d87e48c2602a37b646814f to your computer and use it in GitHub Desktop.
Redux 2
This file contains 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
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