在我的反应项目中,我有一个如下所示的操作文件:
const startLoad = () => async (dispatch) => {
dispatch({
type: CONTENT_LOAD,
});
};
// Get all content
export const getContents = () => async (dispatch) => {
dispatch(startLoad());
// some more code...
};
所以在这个例子中,我知道 dispatch 来自middleware
并且getContents
可以访问它,因为它是使用mapDispatchToProps
. 所以我假设什么时候getContents
被调用,它真的像这样调用 ->dispatch(getContents)
但是这可以用普通的 JavaScript 复制,这样我就可以看到到底发生了什么?如果我对为什么getContents
可以访问有误,dispatch
请告诉我。
例如,如何startLoad
能够使用dispatch
仅仅因为dispatch
被调用startLoad
?我注意到,如果我这样称呼它,它也会起作用:dispatch(startLoad(dispatch));
.
将调度传递给startLoad
实际上对我来说更有意义,所以我不明白为什么不需要这样做。
编辑:这是我自己想出的最接近的例子。
const fun = () => (fun2) => {
fun2();
}
const fun2 = () => {
console.log('hello');
}
fun()(fun2);