函数中的调度参数有什么意义?
What is the point of the dispatch parameter in a function?
刚接触 react 和 redux thunk,只是想知道为什么要有 dispatch 参数
return (dispatch: Dispatch, getState: () => ReduxState):
在 isTest() 方法中???我的理解是调度仅用于调度更新状态的动作,但我下面的方法不需要也没有调度动作。
如果我从方法中删除分派参数,我的网格不会正确更新。
Container file:
getTest: ():any => {
return {
disabledFields: dispatch(isTest("Abc"))
? [grid.title]
: [grid.name]
};
}
Action file:
export const isTest = (
str: String
) => {
return (
dispatch: Dispatch<ReduxState>,
getState: () => ReduxState
): boolean => {
return getState().id === 5;
};
};
来自文档...
Redux Thunk middleware allows you to write action creators that return
a function instead of an action. The thunk can be used to delay the
dispatch of an action, or to dispatch only if a certain condition is
met. The inner function receives the store methods dispatch and
getState as parameters.
所以调度参数的目的是让你可以 运行 在你 return 一个对象到你的减速器之前的一个函数或另一个动作。这方面的一个例子是,如果你用一个动作点击 API,你就可以分派另一个可以解析响应的函数,然后分派一个将 return 解析的响应发送给你的 reducers 的动作。或者它可能不会派发最终操作,因为 API 响应不好。
刚接触 react 和 redux thunk,只是想知道为什么要有 dispatch 参数
return (dispatch: Dispatch, getState: () => ReduxState):
在 isTest() 方法中???我的理解是调度仅用于调度更新状态的动作,但我下面的方法不需要也没有调度动作。
如果我从方法中删除分派参数,我的网格不会正确更新。
Container file:
getTest: ():any => {
return {
disabledFields: dispatch(isTest("Abc"))
? [grid.title]
: [grid.name]
};
}
Action file:
export const isTest = (
str: String
) => {
return (
dispatch: Dispatch<ReduxState>,
getState: () => ReduxState
): boolean => {
return getState().id === 5;
};
};
来自文档...
Redux Thunk middleware allows you to write action creators that return a function instead of an action. The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. The inner function receives the store methods dispatch and getState as parameters.
所以调度参数的目的是让你可以 运行 在你 return 一个对象到你的减速器之前的一个函数或另一个动作。这方面的一个例子是,如果你用一个动作点击 API,你就可以分派另一个可以解析响应的函数,然后分派一个将 return 解析的响应发送给你的 reducers 的动作。或者它可能不会派发最终操作,因为 API 响应不好。