Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created December 8, 2018 19:40
Show Gist options
  • Save runandrerun/7dcc04dad265d21c94d909d8dcf568e0 to your computer and use it in GitHub Desktop.
Save runandrerun/7dcc04dad265d21c94d909d8dcf568e0 to your computer and use it in GitHub Desktop.
const initialToDoState = {
toDos: [],
};
const ADDTODO = {
type: 'ADD_TODO',
toDo: ''
};
const toDoReducer = (state = initialToDoState, action) => {
switch(action.type) {
case 'ADD_TODO':
// spread the current state and toDos array to save prior changes
// add the new toDo to the toDos array
return {
...state,
toDos: [
// spread the array as it's nested
...state.toDos,
action.payload.toDo,
],
};
// default catch in case an action doesn't exist
// this will return the initialState
default:
return state;
};
};
const store = Redux.createStore(toDoReducer, initialToDoState);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment