Created
December 8, 2018 19:49
-
-
Save runandrerun/c04ac3770615384cdd48c9f3f267f7fa to your computer and use it in GitHub Desktop.
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 initialToDoState = { | |
toDos: [], | |
}; | |
const ADDTODO = (toDoValue) => { | |
type: 'ADD_TODO', | |
toDo: toDoValue, | |
}; | |
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.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