Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save justin3737/b26c2008db09b0d40ed78651244cfcd0 to your computer and use it in GitHub Desktop.

Select an option

Save justin3737/b26c2008db09b0d40ed78651244cfcd0 to your computer and use it in GitHub Desktop.

Revisions

  1. justin3737 created this gist Jul 19, 2016.
    21 changes: 21 additions & 0 deletions Redux reducer - add item & remove item
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    function reducer(state = [], action) {
    switch (action.type) {
    case 'ADD_ATTENDEE':
    // Return a new array with old state and added attendee.
    return [{
    name: action.name,
    color: action.color
    },
    ...state
    ];
    case 'REMOVE_ATTENDEE':
    return [
    // Grab state from begging to index of one to delete
    ...state.slice(0, action.index),
    // Grab state from the one after one we want to delete
    ...state.slice(action.index + 1)
    ];
    default:
    return state;
    }
    };