注意:这是一个问题,与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),我在下面添加我自己的答案,希望它有助于那些正在寻找答案的人.