减速器可以听其他动作吗? [英] Is it OK for a reducer to listen to other actions?

查看:19
本文介绍了减速器可以听其他动作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在创建操作,然后是一个 reducer 来处理我的应用程序的不同部分......不同的域.

At the moment I'm creating actions and then a reducer to handle different parts of my app... the different domains.

我的应用会列出班级和学生.

My app lists classes and pupils.

目前我有一个应用程序已加载的操作,以便我知道何时删除加载微调器,我有针对班级和学生的操作.我的问题是我发现我需要连续执行多个操作,但不确定这是否是使用 redux 的有效方法.

Currently I have an action that the app has loaded so that I know when to remove the loading spinner, I have actions for classes and pupils. My problem is that I find I need to execute several actions in a row and am not sure if this is a valid way to use redux.

这是一个示例函数,它在数据加载后分派多个动作:

Here is an example function that dispatches several actions after the data is loaded:

/**
  * Callback used by readAppData.
  *
  * @param object ioResult An object: {success: boolean, errorObj?: object, data?: string}
  */
  dataLoaded(ioResult: Object) {
    if (ioResult.success === true) {
      this.props.dispatch(appActions.appHasLoaded());
      if (ioResult.data !== '') {
        const jsonData = JSON.parse(ioResult.data);
        this.props.dispatch(classActions.replaceClasses(jsonData.classes));
        this.props.dispatch(pupilActions.replacePupils(jsonData.pupils));
      }

    } else if (ioResult.errorObj.code === 'ENOENT') { // File doesn't exist.
      writeAppData('', this.dataSaved);
    } else { // Some other error.
      this.props.dispatch(appActions.appHasErrored());
    }
  }

我正在考虑将 jsonData.classes 和 jsonData.pupils 放入 appActions.appHasLoaded() 调用中,并且在我的班级和学生减速器中有一个新的 APP_HAS_LOADED 案例.

I was thinking about putting the jsonData.classes and jsonData.pupils into the appActions.appHasLoaded() call and just have a new case for APP_HAS_LOADED in my classes and pupils reducers.

这是更好的方法吗?把它减少到一个动作?有单独的动作可以很容易地看到正在发生的事情......也许在 6 个月后,我将不得不查看我的代码,如果我在其他减速器中使用 APP_HAS_LOADED,我将不得不查看它到底发生了什么.此外,在应用程序启动时加载的数据将扩展到不仅仅是班级和学生,所以如果我不合并调用,很快就会有更多的调度 - 也许我可以将数据存储在单独的文件中并加载每个一次一个,这也可以解决我不得不连续调用多个操作的问题.

Is this a better approach? To reduce it down to one action? Having separate actions makes it easy to see what is happening... maybe in 6 months time I will have to look through my code to work out exactly what happens on APP_HAS_LOADED if I use it in other reducers. Also the data that is loaded on app start is going to expand beyond just classes and pupils so if I don't combine the calls there could soon be many more dispatches to make - maybe I can store the data in separate files and load each one one at a time which would also fix my problem of having to call mutiple actions in a row.

多次调用dispatch可以吗?

Is it OK to call dispatch multiple times?

推荐答案

是的,你可以.

来自 Redux 创建者 Dan Abramov:

From Redux creator Dan Abramov:

许多减速器可以处理一个动作.一个 reducer 可以处理很多动作.

Many reducers may handle one action. One reducer may handle many actions.

参考 Redux 文档

此外,在 在 github 上有一个关于此的对话.

also, there is a conversation on github about this.

这篇关于减速器可以听其他动作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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