Skip to content

Instantly share code, notes, and snippets.

@4poc
Last active October 27, 2016 15:59
Show Gist options
  • Select an option

  • Save 4poc/674598e9ac66a1df6948d03488480b17 to your computer and use it in GitHub Desktop.

Select an option

Save 4poc/674598e9ac66a1df6948d03488480b17 to your computer and use it in GitHub Desktop.

Revisions

  1. Matthias Hecker renamed this gist Oct 27, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Matthias Hecker created this gist Oct 27, 2016.
    24 changes: 24 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    // 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;
    }
    }