Skip to content

Instantly share code, notes, and snippets.

@4poc
Last active October 27, 2016 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4poc/674598e9ac66a1df6948d03488480b17 to your computer and use it in GitHub Desktop.
Save 4poc/674598e9ac66a1df6948d03488480b17 to your computer and use it in GitHub Desktop.
// 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