Skip to content

Instantly share code, notes, and snippets.

@RobinMalfait
Created January 14, 2018 18:52
Show Gist options
  • Save RobinMalfait/84b7c7f33a9765e92e84c9a8df747a34 to your computer and use it in GitHub Desktop.
Save RobinMalfait/84b7c7f33a9765e92e84c9a8df747a34 to your computer and use it in GitHub Desktop.
// Switch
const applicationReducer = (state, action) => {
switch (action.type) {
case "FETCH_PACKAGES/START":
case "FETCH_DEPENDENCIES/START":
return {
...state,
fetch_count: state.fetch_count + 1
};
case "FETCH_PACKAGES/SUCCESS":
case "FETCH_PACKAGES/FAILED":
case "FETCH_DEPENDENCIES/SUCCESS":
case "FETCH_DEPENDENCIES/FAILED":
return {
...state,
fetch_count: state.fetch_count + 1
};
default:
return state;
}
};
// My Magic
const applicationReducer = createReducer({
[oneOf("FETCH_PACKAGES", "FETCH_DEPENDENCIES")]: {
START(state, action) {
return {
...state,
fetch_count: state.fetch_count + 1
};
},
[oneOf("FAILED", "SUCCESS")](state, action) {
return {
...state,
fetch_count: state.fetch_count + 1
};
}
}
});
// My Magic + immer
const applicationReducer = createReducer({
[oneOf("FETCH_PACKAGES", "FETCH_DEPENDENCIES")]: {
START(state, action) {
state.fetch_count++;
},
[oneOf("FAILED", "SUCCESS")](state, action) {
state.fetch_count--;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment