我可以在没有Redux Thunk中间件的情况下发送多个动作吗? [英] Can I dispatch multiple actions without Redux Thunk middleware?

查看:165
本文介绍了我可以在没有Redux Thunk中间件的情况下发送多个动作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到Redux Thunk是管理异步操作/请求的可靠方法。通过其他操作调度操作没什么用。

I read that Redux Thunk is the reliable way to manage asynchronous actions/request. There's nothing much about dispatching actions by other actions.

如何调度同步操作?
我不确定thunk方法的性能问题,但是我可以在其他动作创建者中调度动作而不在其中定义函数吗?

How about dispatching synchronous actions? I am not sure of thunk approach's performance issues, but can I just dispatch action inside other action creator without defining function inside?

在我看来,使用redux thunk对于这种需要是不必要的。

It seems to me that using redux thunk is unnecessary for this need.

推荐答案

显示和隐藏通知确实 对于thunk来说似乎是一个很好的用例。

Showing and hiding notification does indeed appear to be a good use case for thunks.

David's答案描述了为响应某些事情而进行多次更新的默认方式:从不同的reducer处理它。大多数情况下,这就是你想要做的事情。

David’s answer describes the "default" way of doing several updates in response to something: handle it from different reducers. Most often this is what you want to do.

有时(和通知一样)它可能不方便。我将介绍如何在我对此问题的回答中选择调度一个或多个操作。

Sometimes (as with notifications) it can be inconvenient. I describe how you can choose between dispatching one or several actions in my answer to this question.

如果你决定分派多个动作,或者只是从你的组件顺序执行,或者使用Redux Thunk。请注意,如果Redux Thunk对你来说似乎很神秘,你应该了解它的真正含义,然后再使用它。它只提供代码组织方面的好处;实际上它与你自己连续两次运行 dispatch()没有什么不同。

In case when you do decide to dispatch multiple actions, either just do it sequentially from your components, or use Redux Thunk. Note that if Redux Thunk seems mysterious to you, you should understand what it really is before using it. It only provides benefits in terms of code organization; in reality it’s not any different from running dispatch() two times in a row yourself.

那说, Redux Thunk调度多个动作如下所示:

That said, with Redux Thunk dispatching multiple actions looks like this:

function increment() {
  return { type: 'INCREMENT' }
}

function incrementTwice() {
  return dispatch => {
    dispatch(increment())
    dispatch(increment())
  }
}

store.dispatch(increment())
incrementTwice()(store.dispatch) // doesn’t require redux-thunk but looks ugly
store.dispatch(incrementTwice()) // requires redux-thunk but looks nice

使用Redux Thunk不会出现任何性能问题。这只是一种调用函数的好方法,你可以将你的调度移交给他们,这样他们可以按照自己的意愿多次执行。

Using Redux Thunk will not have any performance issues. It’s just a nice way of calling functions to which you can hand over your dispatch so they can do it as many times as they want.

这篇关于我可以在没有Redux Thunk中间件的情况下发送多个动作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆