Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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