源代码
redux.js
import parent_reducer from './parent_redux';
import { combineReducers } from 'redux';
export default combineReducers({ parent:parent_reducer });parent_redux.js
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
import { createAction, handleActions } from 'redux-actions';
const initial_state = {
number: 0
};
export default handleActions({
...
}, initial_state);我想要的
在上面的源代码中,当访问数字变量时,如下所示。state.parent.self.number和state.parent.child.number
但我想按以下方式访问它。state.parent.number和state.parent.child.number
这种不便经常发生在按层次使用redux时。
有什么办法可以解决吗?阅读:)
发布于 2018-01-01 12:00:25
redux.js
import parent_reducer from './parent_redux';
import { combineReducers } from 'redux';
export default combineReducers({ parent:parent_reducer });parent_redux.js
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
import { createAction, handleActions } from 'redux-actions';
const initial_state = {
number: 0
};
export default handleActions({
...
}, initial_state);我只是在parent_redux.js中做了一个小改动,同时保持了代码的其余部分不变。
这将为您提供所需的状态结构。
您必须了解一件事,您的两个还原器都是用相同的键number返回一个对象,所以您需要附加一个键self作为额外的,以建立一个父-子关系'Redux‘来构建状态。
相反,如果您返回一个原始值,则存储该值的键可以与前一个还原器返回的对象中的键相同。
希望这能为您解决问题,而不会破坏代码的模式。
https://stackoverflow.com/questions/48049071
复制相似问题