为什么我的 redux 自定义中间件需要 return dispatch on action?
why my redux custom middleware need to return dispatch on action?
我会设置 redux 自定义中间件,并更改操作以插入操作参数。但结果是 actions must be plain objects. use custom middleware for async actions
因为在行动中我没有 return dispatch(myAction)
我的配置中间件
const injectMiddleware = ({dispatch, getState}) => next => action => {
//skipped all my logic
return(
typeof action === 'function' ?
next(action({dispatch, getState, ...anotherCustomFunction}))
:
next(action)
)
}
我的操作
export const setUserSessionToken = () => ({dispatch}: Store) => {
dispatch(setToken)
}
并得到错误
actions must be plain objects. use custom middleware for async actions
固定为 return
export const setUserSessionToken = () => ({dispatch}: Store) => {
return dispatch(setToken)
}
和 no error
没有 return
如果不使用自定义中间件
export const setUserSessionToken = () => (dispatch) => {
dispatch(setToken)
}
或者在自定义中间件中做
return next(action)
改变这个
next(action({dispatch, getState, ...anotherCustomFunction}))
作为
action({dispatch, getState, ...anotherCustomFunction})
调用 next 是将 action 对象传递给 next 中间件。当然,您可以传递一个尚未评估的函数,但您最终需要另一个中间件,如 thunk 来处理该函数。
我会设置 redux 自定义中间件,并更改操作以插入操作参数。但结果是 actions must be plain objects. use custom middleware for async actions
因为在行动中我没有 return dispatch(myAction)
我的配置中间件
const injectMiddleware = ({dispatch, getState}) => next => action => {
//skipped all my logic
return(
typeof action === 'function' ?
next(action({dispatch, getState, ...anotherCustomFunction}))
:
next(action)
)
}
我的操作
export const setUserSessionToken = () => ({dispatch}: Store) => {
dispatch(setToken)
}
并得到错误
actions must be plain objects. use custom middleware for async actions
固定为 return
export const setUserSessionToken = () => ({dispatch}: Store) => {
return dispatch(setToken)
}
和 no error
没有 return
如果不使用自定义中间件
export const setUserSessionToken = () => (dispatch) => {
dispatch(setToken)
}
或者在自定义中间件中做
return next(action)
改变这个
next(action({dispatch, getState, ...anotherCustomFunction}))
作为
action({dispatch, getState, ...anotherCustomFunction})
调用 next 是将 action 对象传递给 next 中间件。当然,您可以传递一个尚未评估的函数,但您最终需要另一个中间件,如 thunk 来处理该函数。