Skip to content

Instantly share code, notes, and snippets.

@jonwaldstein
Created March 16, 2020 19:05
Show Gist options
  • Save jonwaldstein/b4eccfe774c48db5d0a0a0f4f1be9bca to your computer and use it in GitHub Desktop.
Save jonwaldstein/b4eccfe774c48db5d0a0a0f4f1be9bca to your computer and use it in GitHub Desktop.
React context state reducer
const UPDATE_STATE = 'update_state';
export default function reducer(state, action) {
switch (action.type) {
case UPDATE_STATE:
return {
...state,
...action.newData,
};
default:
return state;
}
}
/**
* Set data action
* @param {object} newData
* @returns {{type: string, newData: Object}}
*/
export function setData(newData) {
return {
type: UPDATE_STATE,
newData,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment