Skip to content

Instantly share code, notes, and snippets.

@hunterc
Last active August 31, 2015 20:15
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 hunterc/e8b41e846ffbc3bc476e to your computer and use it in GitHub Desktop.
Save hunterc/e8b41e846ffbc3bc476e to your computer and use it in GitHub Desktop.
reducer stores
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