gpt4 book ai didi

javascript - 我需要在中间件中使用 `next` 还是 `store.dispatch`

转载 作者:行者123 更新时间:2023-11-29 21:36:06 25 4
gpt4 key购买 nike

我正在构建我想从中分派(dispatch)多个操作的异步中间件。这是它目前的样子:

const [ requestType, successType, failureType ] = types;
next({type: requestType});

return call().then(
result => {
return next({
result,
type: successType
})
},
error => {
// I want to dispath the `sendNotification()` action here
// to add item to notifications center
// should I use `next` or `store.dispath`?

//this way?
store.dispath(failedNotification);
//or this way?
next(failedNotification);

return next({
type: failureType,
error: error.message || 'Something bad happened',
errorType: error.errorType
})
}
)

我的问题在上面代码的注释中:

      // I want to dispath the `sendNotification()` action here
// to add item to notifications center
// should I use `next` or `store.dispath`?

我问的原因是因为根据我对 applyMiddleware 实现细节的理解,来自 here , next() 将通过剩余的中间件链传递一个 Action ,而 store.dispatch 将通过包括当前中间件在内的整个中间件链。

最佳答案

如果您使用 store.dispatch(),调度将再次触发您的中间件并再次调用 call()。有时它可能会导致无限循环,因此调用 next() 在这里看起来是更好的选择。

关于javascript - 我需要在中间件中使用 `next` 还是 `store.dispatch`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34836302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com