1
1
import mapValues from '../utils/mapValues' ;
2
2
3
+ function bindActionCreator ( actionCreator , dispatch ) {
4
+ return ( ...args ) => dispatch ( actionCreator ( ...args ) ) ;
5
+ }
6
+
3
7
/**
4
8
* Turns an object whose values are action creators, into an object with the
5
9
* same keys, but with every function wrapped into a `dispatch` call so they
6
10
* may be invoked directly. This is just a convenience method, as you can call
7
11
* `store.dispatch(MyActionCreators.doSomething())` yourself just fine.
8
12
*
9
- * @param {Object } actionCreators An object whose values are action creator
10
- * functions. One handy way to obtain it is to use ES6 `import * as` syntax.
13
+ * @param {Object|Function } actionCreators An object whose values are action
14
+ * creator functions. One handy way to obtain it is to use ES6 `import * as`
15
+ * syntax. It also supports binding only a single function.
11
16
*
12
17
* @param {Function } dispatch The `dispatch` function available on your Redux
13
18
* store.
@@ -16,7 +21,11 @@ import mapValues from '../utils/mapValues';
16
21
* action creator wrapped into the `dispatch` call.
17
22
*/
18
23
export default function bindActionCreators ( actionCreators , dispatch ) {
24
+ if ( typeof actionCreators === 'function' ) {
25
+ return bindActionCreator ( actionCreators , dispatch ) ;
26
+ }
27
+
19
28
return mapValues ( actionCreators , actionCreator =>
20
- ( ... args ) => dispatch ( actionCreator ( ... args ) )
29
+ bindActionCreator ( actionCreator , dispatch )
21
30
) ;
22
31
}
0 commit comments