Skip to content

Instantly share code, notes, and snippets.

@treyhuffine
Last active January 3, 2019 12:50
Show Gist options
  • Save treyhuffine/41908b173b585a6ec5a7472b83d62b97 to your computer and use it in GitHub Desktop.
Save treyhuffine/41908b173b585a6ec5a7472b83d62b97 to your computer and use it in GitHub Desktop.
A Redux reducer to manage a to-do list
// This will be fed into the reducer when the app loads to initialize the state
const getInitialState = () => ({
todoList: [],
});
const reducer = (prevState = getInitialState(), action) => {
switch (action.type) {
case 'ADD_TODO':
const nextState = {
todoList: [
...prevState.todoList,
action.text,
],
};
return nextState;
default:
return prevState;
};
// console.log(store.getState()) => { todoList: [] }
//
// store.dispatch({
// type: 'ADD_TODO',
// text: 'Get milk from the store',
// });
//
// console.log(store.getState()) => { todoList: ['Get milk from the store'] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment