Sha256: a9e6a42c41f392892588ad7156a033702270da6f69fa4b35048be56c3eca9d5b

Contents?: true

Size: 1.91 KB

Versions: 17

Compression:

Stored size: 1.91 KB

Contents

import { kindOf } from './utils/kindOf'

function bindActionCreator(actionCreator, dispatch) {
  return function () {
    return dispatch(actionCreator.apply(this, arguments))
  }
}

/**
 * Turns an object whose values are action creators, into an object with the
 * same keys, but with every function wrapped into a `dispatch` call so they
 * may be invoked directly. This is just a convenience method, as you can call
 * `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
 *
 * For convenience, you can also pass an action creator as the first argument,
 * and get a dispatch wrapped function in return.
 *
 * @param {Function|Object} actionCreators An object whose values are action
 * creator functions. One handy way to obtain it is to use ES6 `import * as`
 * syntax. You may also pass a single function.
 *
 * @param {Function} dispatch The `dispatch` function available on your Redux
 * store.
 *
 * @returns {Function|Object} The object mimicking the original object, but with
 * every action creator wrapped into the `dispatch` call. If you passed a
 * function as `actionCreators`, the return value will also be a single
 * function.
 */
export default function bindActionCreators(actionCreators, dispatch) {
  if (typeof actionCreators === 'function') {
    return bindActionCreator(actionCreators, dispatch)
  }

  if (typeof actionCreators !== 'object' || actionCreators === null) {
    throw new Error(
      `bindActionCreators expected an object or a function, but instead received: '${kindOf(
        actionCreators
      )}'. ` +
        `Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
    )
  }

  const boundActionCreators = {}
  for (const key in actionCreators) {
    const actionCreator = actionCreators[key]
    if (typeof actionCreator === 'function') {
      boundActionCreators[key] = bindActionCreator(actionCreator, dispatch)
    }
  }
  return boundActionCreators
}

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
isomorfeus-redux-4.2.0 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.18 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.17 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.16 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.15 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.14 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.13 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.12 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.11 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.10 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.9 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.8 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.7 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.6 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.5 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.4 node_modules/redux/src/bindActionCreators.js
isomorfeus-redux-4.1.3 node_modules/redux/src/bindActionCreators.js