Skip to content

Commit aa3fbe3

Browse files
committed
bindActionCreators
1 parent 14cf3e6 commit aa3fbe3

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/utils/bindActionCreators.js

-10
This file was deleted.

src/utils/bindActionCreators.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ActionCreator, ActionCreatorsMapObject, AnyAction, Dispatch } from "redux"
2+
3+
function bindActionCreator<A extends AnyAction = AnyAction>(
4+
actionCreator: ActionCreator<A>,
5+
dispatch: Dispatch
6+
) {
7+
return function (this: any, ...args: any[]) {
8+
return dispatch(actionCreator.apply(this, args))
9+
}
10+
}
11+
12+
export default function bindActionCreators(actionCreators: ActionCreator<any> | ActionCreatorsMapObject, dispatch: Dispatch) {
13+
if (typeof actionCreators === 'function') {
14+
return bindActionCreator(actionCreators, dispatch)
15+
}
16+
17+
const boundActionCreators: ActionCreatorsMapObject = {}
18+
for (const key in actionCreators) {
19+
const actionCreator = actionCreators[key]
20+
if (typeof actionCreator === 'function') {
21+
boundActionCreators[key] = (...args) => dispatch(actionCreator(...args))
22+
}
23+
}
24+
return boundActionCreators
25+
}

0 commit comments

Comments
 (0)