Last active
October 27, 2016 15:59
-
-
Save 4poc/674598e9ac66a1df6948d03488480b17 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
// immutable object assignment: | |
const initialState = { products: {} }; | |
export default function reducer(state = initialState, action = {}) { | |
switch (action.type) { | |
case PUT_PRODUCT: | |
return { ...state, products: { ...state.products, [action.product.id]: action.product } }; | |
default: | |
return state; | |
} | |
} | |
// immutable array insertion: | |
// action.number (number to insert), action.index (array index to insert at) | |
const initialState = { numbers: [] }; | |
export default function reducer(state = initialState, action = {}) { | |
switch (action.type) { | |
case PUT_NUMBER: | |
return { ...state, numbers: [ ...state.numbers.slice(0, action.index), | |
action.number, | |
...state.numbers.slice(action.index + 1) ] }; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment