Created
November 20, 2015 16:18
-
-
Save tomkis/236f6ba182b48fde4dc9 to your computer and use it in GitHub Desktop.
what if your reducers were generators?
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
// 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