Skip to content

Instantly share code, notes, and snippets.

@hpstuff
Created March 30, 2018 13:25
Show Gist options
  • Save hpstuff/8b6e0d4981bbc871270ca9108d6ff947 to your computer and use it in GitHub Desktop.
Save hpstuff/8b6e0d4981bbc871270ca9108d6ff947 to your computer and use it in GitHub Desktop.
bindActionCreators with wix navigator included
import { bindActionCreators } from 'redux';
const bindNavigationActionCreator = (actionCreator, navigator) =>
(...args) => ({...actionCreator.apply(this, args), navigator});
export const bindNavigationActionCreators = (actionCreators, dispatch, navigator) => {
if (typeof actionCreators === 'function') {
const boundActionCreator = bindNavigationActionCreator(actionCreators, navigator);
return bindActionCreators(boundActionCreator, dispatch);
}
if (typeof actionCreators !== 'object' || actionCreators === null) {
throw new Error(
`bindActionCreators expected an object or a function, instead received ${
actionCreators === null ? 'null' : typeof actionCreators
}. ` +
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
)
}
const boundActionCreators = Object.keys(actionCreators).reduce((actions, key) => {
const actionCreator = actionCreators[key];
if (typeof actionCreator === 'function') {
actions[key] = bindNavigationActionCreator(actionCreator, navigator);
}
return actions;
}, {});
return bindActionCreators(boundActionCreators, dispatch);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment