Redux 和在减速器中访问不同的减速器,如何?

问题描述:

所以,我有一个应用有多个减速器,因此有多个动作创建器相关联.

So, I have an app that has multiple reducers, and thus multiple action creators associated.

有一段时间我的一个减速器更新了状态(因为编辑),因此,我必须确保其他减速器看到这个更新并相应地调整他们的状态.

There is a time in which one of my reducers updates the state (because of an edit), and as such, I have to then make sure the other reducers see this update and adjust their state accordingly.

把它想象成一个用户选择了运动鞋",这个状态管理在一个叫做 productsReducer 的 reducer 中.因此,当用户通过应用程序流程时,他们会获得与之相关联的couponsReducer……但用户可以随时编辑",所以如果他们随后编辑运动鞋"并说,选择另一个制作人,我必须能够实时更改couponsReducer,因为进度模式需要反映这一点.我可以通过进入优惠券页面的流程来做到这一点,因为在该页面上我只是协调了 componentWillMount 上的数据......但问题是我有一个进度模式,在进入优惠券页面之前,用户可以打开进度模式...这里会有一个不匹配...

think of it as if a user selects "sneakers", and this state management is in a reducer called productsReducer. As such, as a user moves thru the app process, they get couponsReducer associated with it... but the user can "edit" at any time, so if they then edit "sneakers" and say, choose another producer, I have to be able to alter the couponsReducer in real time, because a progress modal needs to reflect this. I can do this via going thru the flow to the coupon page, because on that page I just reconcile the data on componentWillMount... but the rub is I have a progress modal, and prior to getting to the coupons page, a user can open the progress modal... and there will be a missmatch here...

啰嗦我知道,我只是想解释一下我的情况.

long winded I know, I just wanted to explain my situation.

所以,我希望能够从 productsReducer 中调用其他减速器"或它们的动作创建者.

so, I want to be able to call "other reducers" or their action creators from within the productsReducer.

中间件在这里是否合适,我在其中嗅探某个action.type",如果我看到它,我可以分派其他 action.type,或者有没有办法从 productsReducer 中调用动作创建者?

Would a middleware be appropriate here, in which I sniff for a certain "action.type" and if I see it, I can dispatch other action.type's or is there a way to call action creators from within the productsReducer?

importing: import * ascoupons from './couponsActions' 和我需要的任何其他操作似乎不合适.

importing: import * as coupons from './couponsActions' and any other actions I need seem inappropriate.

根据应用程序的状态更新状态是一种反模式.解决方案更简单:

Updating state based on the state of application is an anti-pattern. The solution is even simpler:

如果一个操作影响多个 reducer,请在关心的 reducer 中订阅该操作.

If an action affects multiple reducers, subscribe to that action in the reducers that care.

所以当用户选择sneakers时,触发这些动作

So when a user selects sneakers, fire these actions

{ type: 'item_select', payload: 'sneakers' }

然后在 couponsReducer 中,有这样的东西:

and then in couponsReducer, have something like this:

function (state = initialState, action) {
  if (action.type == 'item_select') {
    return { ...state, activeProduct: action.payload);}
  }
  return state;
}

您不希望级联更新,其中状态取决于其他状态的变化.您需要一个简单的调度架构,其中一个操作同时影响所有内容.

You do not want cascading updates, where states depend on other states changing. You want a simple dispatch architecture where one action affects everything all at once.