小编Jev*_*eni的帖子

使用组合减速器时,访问该州的其他部分

注意:这是一个问题,与Redux 非常相似; 访问其他部分... 它与路由器无关:(因此无法以相同的方式解决

当我减少状态的一部分时,我"觉得"我也需要访问其他部分.我承认我可能会"误解"Redux的核心主体,或者在我的应用程序架构中存在缺陷.

我目前的解决方案是修改github:combineReducers.js的代码:

var finalState = mapValues(finalReducers, (reducer, key) => {
   var previousStateForKey = state[key]
   var nextStateForKey = reducer(previousStateForKey, action)
   ...
}
Run Code Online (Sandbox Code Playgroud)

   var nextStateForKey = reducer(previousStateForKey, action)
Run Code Online (Sandbox Code Playgroud)

   var nextStateForKey = reducer(previousStateForKey, action, state)
Run Code Online (Sandbox Code Playgroud)

这将允许我做我需要的:

function reducer(state, action, root) {
   if (root.otherPart.get("isSomething")) {
      return state.set("anotherThing", true);
   }
   return state;
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我是正确的方式,或者是应该使用不同的架构方法解决的问题,而不需要从其他部分访问状态的一部分?

**更新2018年12月5日**

由于这个问题的相对较高的兴趣(15 up-votes atm),我在下面添加我自己的答案,希望它有助于那些正在寻找答案的人.

javascript redux

18
推荐指数
1
解决办法
8850
查看次数

标签 统计

javascript ×1

redux ×1