Skip to content

Instantly share code, notes, and snippets.

@hunterc hunterc/reducers.js
Last active Aug 31, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.