[英]React Redux dispatch syntax
Let's say I have an action: 假设我有一个动作:
export const getInfoFor = user => {
return dispatch => {
dispatch( fetchApi(user) );
}
}
const fetchApi = user => dispatch => {
return( dispatch({type: SET_USER}) ) <--- ??
}
My questions are, how did dispatch
gets passed to the return
in fetchApi
? 我的问题是, dispatch
如何传递给fetchApi
的return
fetchApi
? Maybe what's throwing me off is the double arrow functions in fetchApi
. 也许让我失望的是fetchApi
的双箭头功能。
The code works, but I want to understand why it works. 该代码有效,但是我想了解它为什么有效。
Your top-level function is intercepted by Redux-Thunk middleware and is passed dispatch, getState, customValues
. 您的顶层函数被Redux-Thunk中间件拦截,并传递给dispatch, getState, customValues
。
An example of the custom value ( from redux-thunk github ) 自定义值的示例(来自redux-thunk github)
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument(api))
)
// later
function fetchUser(id) {
return (dispatch, getState, api) => {
// you can use api here
}
}
So, in short, it comes from Redux-Thunk middleware when you return a function. 简而言之,当您返回函数时,它来自Redux-Thunk中间件。
https://github.com/reduxjs/redux-thunk https://github.com/reduxjs/redux-thunk
https://github.com/reduxjs/redux-thunk/blob/master/src/index.js ( provided by @Nicholas Tower ) https://github.com/reduxjs/redux-thunk/blob/master/src/index.js (由@Nicholas Tower提供)
Redux thunk is a small middleware that checks the typeof
of the action
that gets dispatched to the store. 终极版的thunk是一个小中间件检查typeof
中的action
是被分派到店里来。 If the typeof
action is a function
, the middleware calls the function, passing dispatch as a parameter. 如果typeof
动作是一个function
,则中间件调用该函数,并将dispatch传递为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.