根据另一个减速器的状态更新一个减速器的状态 [英] Update the state of one reducer based on state of another reducer

查看:44
本文介绍了根据另一个减速器的状态更新一个减速器的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在减速器中有一个状态,其状态如下所示:

I've got an state in a reducer with a state that looks like this:

此状态处理当前用户的选择.

This state handles the current user selections.

const selection = {
  timespan: "-3660",
  customTimespan: false,
  pathIds: [''],
  source: undefined,
  direction: 0,
  appClassIds: []
};

我在另一个减速器中有另一个状态,它有一个项目 appClasses 列表,用户可以在树上打开或关闭这些项目.

And I have another state in another reducer that has a list of items appClasses that the user can toggle on or off on a tree.

状态是 appClasses.

The state is appClasses.

问题是,当用户选择某些内容时,我会更新 appClasses 状态,但我也必须根据 appClasses 的新状态更新 selection.appClassIds.

The thing is that when the user selects something I update appClasses state but also I have to update selection.appClassIds depending on the new state of appClasses.

我的问题是如何根据另一个减速器的状态更新减速器的状态?还是我必须将这些状态合并为一个状态?

My question is how can I update a state of a reducer based on the state of another reducer? Or do I have to combine the states in one state?

类似于:

const initialState = {
appClasses = [],
selection: {
  timespan: "-3660",
  customTimespan: false,
  pathIds: [''],
  source: undefined,
  direction: 0,
  appClassIds: []
 }
};

推荐答案

你可以这样做:

function a(state, action) { }
function b(state, action, a) { } // depends on a's state

function something(state = {}, action) {
  let a = a(state.a, action);
  let b = b(state.b, action, a); // note: b depends on a for computation
  return { a, b };
}

干杯:)

这篇关于根据另一个减速器的状态更新一个减速器的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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