首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我能像这样进入一个州吗?

我能像这样进入一个州吗?
EN

Stack Overflow用户
提问于 2018-01-01 10:56:19
回答 1查看 84关注 0票数 1

源代码

redux.js

代码语言:javascript
复制
import parent_reducer from './parent_redux';
import { combineReducers } from 'redux';

export default combineReducers({ parent:parent_reducer });

parent_redux.js

代码语言:javascript
复制
import { combineReducers } from 'redux';
import { createAction, handleActions } from 'redux-actions';
import child_reducer from './child_redux';

const initial_state = {
    number: 0
};

const parent_reducer = handleActions({
    ...
}, initial_state);

export default combineReducers({ self: parent_reducer, child: child_reducer });

child_reudx.js

代码语言:javascript
复制
import { createAction, handleActions } from 'redux-actions';

const initial_state = {
    number: 0
};

export default handleActions({
    ...
}, initial_state);

我想要的

在上面的源代码中,当访问数字变量时,如下所示。state.parent.self.numberstate.parent.child.number

但我想按以下方式访问它。state.parent.numberstate.parent.child.number

这种不便经常发生在按层次使用redux时。

有什么办法可以解决吗?阅读:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-01 12:00:25

redux.js

代码语言:javascript
复制
import parent_reducer from './parent_redux';
import { combineReducers } from 'redux';

export default combineReducers({ parent:parent_reducer });

parent_redux.js

代码语言:javascript
复制
import { combineReducers } from 'redux';
import { createAction, handleActions } from 'redux-actions';
import child_reducer from './child_redux';

const initial_parent_state = 0;

const parent_reducer = handleActions({
   ...
}, initial_parent_state);

export default combineReducers({ number: parent_reducer, child: 
child_reducer });

child_reudx.js

代码语言:javascript
复制
import { createAction, handleActions } from 'redux-actions';

const initial_state = {
  number: 0
};

export default handleActions({
   ...
}, initial_state);

我只是在parent_redux.js中做了一个小改动,同时保持了代码的其余部分不变。

这将为您提供所需的状态结构。

您必须了解一件事,您的两个还原器都是用相同的键number返回一个对象,所以您需要附加一个键self作为额外的,以建立一个父-子关系'Redux‘来构建状态。

相反,如果您返回一个原始值,则存储该值的键可以与前一个还原器返回的对象中的键相同。

希望这能为您解决问题,而不会破坏代码的模式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48049071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档