使用组合减速器时,访问该州的其他部分 [英] Accessing other parts of the state, when using combined reducers

查看:101
本文介绍了使用组合减速器时,访问该州的其他部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NB 这是一个非常类似于终极版;访问其他部分...... 它与路由器无关:(因此无法以相同方式解决



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

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

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

来自

  var nextStateForKey = reducer(previousStateForKey,action)

to

  var nextStateForKey = reducer(previ ousStateForKey,action,state)

这将允许我做我需要的事情:

  function reducer(state,action,root){
if(root.otherPart.get(isSomething)){
return state.set(anotherThing,true);
}
返回状态;
}

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



** UPDATE 2018年12月5日 **



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

解决方案

您不必使用 combineReducers()



来自官方Redux文档


这个助手只是方便!您可以自己编写
combineReducers 不同的
,甚至手动从子减速器组装状态
对象并显式写一个减少
函数的函数,就像你写任何其他函数一样。



您可以在reducer层次结构的任何级别调用 combineReducers
不必在顶部发生。事实上,你可以再次使用它来
将那些过于复杂的儿童减少者分成独立的
孙子,依此类推。


所以你建议的解决方案肯定是你可以做到的一种方式。



感谢@MichelleTilley带头思路!


NB: This is a question, very similar to Redux; accessing other parts... but it has nothing to do with Router :( thus cannot be solved same way

When I reduce one part of the state, I "feel" like I need to access other parts as well. I admit that I just might "misfeel" the core principals of the Redux, or have flaws in my app's architecture.

My current solution would be to modify github:combineReducers.js's code:

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

from

   var nextStateForKey = reducer(previousStateForKey, action)

to

   var nextStateForKey = reducer(previousStateForKey, action, state)

which would allow me to do what I need:

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

Question is if I am on the right way to do it, or is it something that should be solved using different architectural approaches, without having a need to access one part of state from other parts?

** UPDATE 5h Dec, 2018 **

Due to relatively high interest to this question (15 up-votes atm), I am adding my own answer below, hope it helps to those who is looking for that answer.

解决方案

You don't have to use combineReducers().

From the official Redux docs:

This helper is just a convenience! You can write your own combineReducers that works differently, or even assemble the state object from the child reducers manually and write a root reducing function explicitly, like you would write any other function.

You may call combineReducers at any level of the reducer hierarchy. It doesn’t have to happen at the top. In fact you may use it again to split the child reducers that get too complicated into independent grandchildren, and so on.

So your proposed solution is definitely one way you can do it.

Thanks to @MichelleTilley for spearheading the train of thought!

这篇关于使用组合减速器时,访问该州的其他部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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