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

查看:33
本文介绍了我可以在没有 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 的回答 描述了为响应某事而进行多次更新的默认"方式:从不同的减速器处理它.大多数情况下,这就是您想要做的.

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 不会有任何性能问题.这只是一种调用函数的好方法,您可以将 dispatch 交给它,这样他们就可以根据需要多次调用.

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天全站免登陆