[英]Thunk Middleware: how/why does action creator have access to dispatch when it's not passed in?
For reference, this is the origin of the below screenshots: https://github.com/gopinav/React-Redux-Tutorials/blob/master/redux-demo/asyncActions.js作为参考,这是以下屏幕截图的来源: https://github.com/gopinav/React-Redux-Tutorials/blob/master/redux-demo/asyncActions.js
Looking at the fetchUsers
action creator function in the below screenshot, how is dispatch
passed into this function?查看下面屏幕截图中的
fetchUsers
操作创建者 function, dispatch
是如何传递到这个 function 中的? Not clear how the fetchUsers
function has access to dispatch and can pass it into the anonymous return function within fetchUsers
.不清楚
fetchUsers
function 如何访问 dispatch 并将其传递到 fetchUsers 中的匿名返回fetchUsers
。
The store
creation in the 2nd screenshot is where the thunkMiddleware
is applied and the fetchUsers
is dispatched.第二张截图中的
store
创建是应用thunkMiddleware
和分派fetchUsers
的地方。
@JLRishe answered this correctly in a comment. @JLRishe 在评论中正确回答了这个问题。
fetchUsers
is a function that returns an anonymous function. The anonymous function takes the dispatch
function as an argument. fetchUsers
是一个返回匿名 function 的 function。匿名 function 将dispatch
function 作为参数。 When you call store.dispatch(fetchUsers())
, note that you're passing the return value of fetchUsers
to store.dispatch
.当您调用
store.dispatch(fetchUsers())
时,请注意您正在将fetchUsers
的返回值传递给store.dispatch
。 Redux then calls this anonymous function, passing it the dispatch
method as an argument. Redux 然后调用这个匿名 function,将
dispatch
方法作为参数传递给它。 The anonymous function is then invoked and can use the passed in dispatch
function to dispatch additional actions.然后调用匿名 function 并可以使用传入的
dispatch
function 来调度其他操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.