Created
January 14, 2018 18:52
-
-
Save RobinMalfait/84b7c7f33a9765e92e84c9a8df747a34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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