Skip to content

Instantly share code, notes, and snippets.

@jeffscottward
Created August 5, 2019 15:55
Show Gist options
  • Save jeffscottward/72044c0a08933eeb5f6df0ff3bde7945 to your computer and use it in GitHub Desktop.
Save jeffscottward/72044c0a08933eeb5f6df0ff3bde7945 to your computer and use it in GitHub Desktop.
Reducer to use with useStateValue
// Inside component use
// const [{data}, dispatch] = useStateValue();
// REDUCER
export default function (state, action) {
switch (action.type) {
case "SET_DATA":
return {
...state,
data: action.newData
};
case "SET_VISIBLE_DATA":
return {
...state,
visibleData: action.newVisibleData
};
case "SET_FONT_COLORS":
let newFontState = {};
if (action.numberType === "odd") {
switch (state.fontColors.odd) {
case "black":
newFontState = {
...state.fontColors,
[action.numberType]: "orange"
};
break;
case "orange":
newFontState = {
...state.fontColors,
[action.numberType]: "black"
};
break;
default:
break;
}
}
if (action.numberType === "even") {
switch (state.fontColors.even) {
case "black":
newFontState = {
...state.fontColors,
[action.numberType]: "green"
};
break;
case "green":
newFontState = {
...state.fontColors,
[action.numberType]: "black"
};
break;
default:
break;
}
}
return {
...state,
fontColors: newFontState
};
case "SET_COLUMN_SORT_DIRECTIONS":
return {
...state,
columnSortDirections: action.newColumnSortDirections
};
default:
break;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment