Skip to content

Instantly share code, notes, and snippets.

@frankfaustino
Created March 21, 2018 19:41
Show Gist options
  • Save frankfaustino/9b8105eb0c3597bf3e47b4935d2b3c7b to your computer and use it in GitHub Desktop.
Save frankfaustino/9b8105eb0c3597bf3e47b4935d2b3c7b to your computer and use it in GitHub Desktop.
Redux explained as a reduce function
const state = []
// Actions => tells the reducer which action to perform and provides the values
const action = [
{ type: 'ADD_TODO', text: 'do laundry', completed: false, id: 0 },
{ type: 'ADD_TODO', text: 'walk the kids', completed: false, id: 1 },
{ type: 'ADD_TODO', text: 'take the dogs to school', completed: false, id: 2 },
{ type: 'ADD_TODO', text: 'kiss the plant', completed: false, id: 3 },
{ type: 'ADD_TODO', text: 'water the wifey', completed: false, id: 4 },
{ type: 'REMOVE_TODO', id: 0 },
{ type: 'REMOVE_TODO', id: 3 },
{ type: 'TOGGLE_TODO', id: 4 }
]
// Reducer => does all the work and returns a new state
function reducer(state, action) {
switch(action.type) {
case 'ADD_TODO':
const { text, completed, id } = action
return [...state, { text, completed, id }]
case 'REMOVE_TODO':
return [...state.filter(todo => todo.id !== action.id)]
case 'TOGGLE_TODO':
const { completed } = action
return state.map(todo =>
todo.id === action.id ? {...todo, completed: !completed } : todo)
default:
return state
}
}
action.reduce(reducer, state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment