Skip to content

Instantly share code, notes, and snippets.

@runandrerun
Created December 8, 2018 19:56
Show Gist options
  • Save runandrerun/11a78cb577f6391c97d8efbe71f17820 to your computer and use it in GitHub Desktop.
Save runandrerun/11a78cb577f6391c97d8efbe71f17820 to your computer and use it in GitHub Desktop.
// remember that we have a store that we can call on to check the state
const store = Redux.createStore(toDoReducer, initialToDoState);
// check the state
store.getState()
// initial state
{ toDos: [] }
// action ADD_TODO is dispatched with the value 'Buy Food'
const ADDTODO = ('Buy Food') => {
type: 'ADD_TODO',
toDo: 'Buy Food',
};
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,
'Buy Food',
],
};
// default catch in case an action doesn't exist
// this will return the initialState
default:
return state;
};
};
// check state after action is dispatched and completed
store.getState()
{ toDos: [ 'Buy Food' ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment