Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Created March 18, 2021 21:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cassidoo/6479f5ea04aa4012619d3a91f478e0f6 to your computer and use it in GitHub Desktop.
Save cassidoo/6479f5ea04aa4012619d3a91f478e0f6 to your computer and use it in GitHub Desktop.
Reducer content
let array = [1,2,3,4,5]
10 + 1
11 + 2
13 + 3
16 + 4
20 + 5
let initialState = {
count: 0,
cake: 'fishy'
}
// dispatch
let actions = [
{ type: "ADD", by: 40 },
{ type: "MINUS", by: 10 },
{ type: "BAKE_CAKE" }
]
let reducer = (state, action) => {
switch(action.type) {
case "ADD": {
return {...state, count: state.count + action.by}
}
case "MINUS": {
return {...state, count: state.count - action.by}
}
case "BAKE_CAKE": {
return { ...state, cake: "a lie" }
}
default: {
return state
}
}
}
let sum = actions.reduce(reducer, initialState)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment