Last active
August 31, 2015 20:15
-
-
Save hunterc/e8b41e846ffbc3bc476e to your computer and use it in GitHub Desktop.
reducer stores
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
function initialState() { | |
return []; | |
} | |
function selectReducer(state = initialState(), action) { | |
switch (action.type) { | |
case SELECT_OPTION: | |
return state.map(option => | |
option.id === action.id | |
? { ...option, selected: true } | |
: option | |
); | |
case GET_OPTIONS: | |
return { ...state, options: [...action.options] }; | |
default: | |
return state; | |
} | |
} | |
function multiselectReducer(state = initialState(), action) { | |
switch (action.type) { | |
case SELECT_OPTIONS: | |
return state.map(option => | |
_.includes(action.selected, option.id) | |
? { ...option, selected: true } | |
: option; | |
case GET_OPTIONS: | |
return { ...state, options: [...action.options] }; | |
default: | |
return state; | |
} | |
} | |
// first time reducer is run the initial state is returned | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment