Last active
May 23, 2016 08:58
-
-
Save LucaColonnello/fbc9faa28f8db8a9adba12db9dbdda56 to your computer and use it in GitHub Desktop.
Redux bind nested action creators
This file contains hidden or 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
| import { bindActionCreator } from 'redux'; | |
| export default function bindNestedActionCreators(actions, dispatch) { | |
| if (typeof actions !== 'function' && (typeof actions !== 'object' || actions === null)) return false; | |
| let dispatchedActions; | |
| if (typeof actions === 'object') { | |
| dispatchedActions = {}; | |
| Object.keys(actions).forEach(a => { | |
| const newActions = bindNestedActionCreators(actions[a], dispatch); | |
| if (newActions) dispatchedActions[a] = newActions; | |
| }); | |
| } | |
| if (typeof actions === 'function') { | |
| dispatchedActions = bindActionCreator(actions, dispatch); | |
| } | |
| return dispatchedActions; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment