Skip to content

Instantly share code, notes, and snippets.

@tomkis
Created November 20, 2015 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkis/236f6ba182b48fde4dc9 to your computer and use it in GitHub Desktop.
Save tomkis/236f6ba182b48fde4dc9 to your computer and use it in GitHub Desktop.
what if your reducers were generators?
// You can defer the side-effect execution in middlewares
const sideEffect = appState => dispatch => localStorage.setItem('counter', appState);
function plainOldReducer(appState) {
return appState + 1;
}
// Composition simply work
function nestedReducer*(appState, action) {
if (action === FOO) {
yield sideEffect(appState);
return plainOldReducer(appState);
} else {
return appState;
}
}
function rootReducer*(appState = 0, action) {
return yield* nestedReducer(appState, action);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment