kilto
Version:
A state management system with easy async and low boilerplate.
16 lines (13 loc) • 747 B
JavaScript
import mapObjectToObject from './util/mapObjectToObject';
// This function automatically binds the actions of the second argument to dispatch.
// This function does not bind the third argument, those are expected to call dispatch themselves,
// allowing you to use this function even if not every single action can be auto-bound.
export default function bindActions(dispatch, actions, preBoundActions = {}) {
if (typeof dispatch !== 'function' || typeof actions !== 'object' || actions === null) {
throw new Error('Make sure you pass dispatch FIRST To bindActions.');
}
const result = mapObjectToObject(actions, (name, fn) =>
[name, (...args) => dispatch(fn, ...args)]
);
return Object.assign({}, result, preBoundActions);
}