Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Created June 30, 2019 15:21
Show Gist options
  • Save mattlockyer/872c4730d27d0e8fd7cdee589d36716d to your computer and use it in GitHub Desktop.
Save mattlockyer/872c4730d27d0e8fd7cdee589d36716d to your computer and use it in GitHub Desktop.
Generate functions that are based on a similar structure by name - e.g. set redux state of UI components
//example functions to set redux state of UI components
const {setDrawerState, setDialogState, setPopoverState} = ['Drawer', 'Dialog', 'Popover'].map((name) => ({
['set' + name + 'State']: (state) => async (dispatch, getState) => {
const stateName = name.toLowerCase() + 'State'
const currentState = getState().appReducer[stateName]
dispatch({ type: 'UPDATE_UI_STATE', [stateName]: { ...currentState, ...state } })
}
})).reduce((a, c) => ({...a, ...c}))
export { setDrawerState, setDialogState, setPopoverState }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment