reactjs - 如何在 ReactRedux 中使用 combineReducer?并且 combineReducers 的 'sate' 对象到多个 reduce
问题描述
我最近开始研究 react web 开发应用程序,并开始在我的应用程序中使用 react redux saga。所以问题是我创建了两个reducer reducer1,reducer2并使用了 combineReducers但状态没有发送回我的Component上的propsToState。
注意:如果我使用单个减速器,它工作正常
示例代码:
我的店铺
// create the saga middleware
const sagaMiddleware = createSagaMiddleware();
const rootReducers = combineReducers({reduce1, reducer2})
// create a redux store with our reducer above and middleware
let store = createStore(
rootReducers,
compose(applyMiddleware(sagaMiddleware))
);
// run the saga
function* rootSaga () {
yield all([
fork(saga1),
fork(saga2),
]);
}
sagaMiddleware.run(rootSaga);
我的减速机
// reducer with initial state
const initialState = {
p1: false,
p2: null,
p3: null
};
export function reducer1(state = initialState, action) {
console.log(state);
switch (action.type) {
case ACTION1:
return { ...state, p1: true, p3: null };
case ACTION2:
const lState = { ...state, p1: false, p2: action.data };
return lState;
default:
return state;
}
}
注意:并且 reduce2 类似于 reducer1
在我的组件上
const mapStateToProps = state => {
return {
loggining: state.loggining,
user: state.user,
error: state.error
};
};
解决方案
在获取映射到特定减速器的状态时,您缺少应该是减速器名称的键。
const mapStateToProps = state => {
return {
loggining: state.reducer1 .loggining, //add key reducer1
user: state.reducer1 .user,
error: state.reducer1.error
};
};
请找到更多details here
推荐阅读
- typescript - 如何在 ts 项目中提供手工编写的声明文件?
- spring-boot - 在百里香模板中显示值
- animation - 如何在 Kotlin 中为 Kotlin 中的场景动画调试“java.lang.NoSuchMethodError”?
- python - 在 Cython 中使用 C++ 的`str.erase()`
- highcharts - 在同一页面中显示多个图表
- javascript - 如何在画布面料JS上绘制线条动画
- python - 无法在 tensorflow-gpu 上使用 GPU:“无法创建 cudnn 句柄:CUDNN_STATUS_INTERNAL_ERROR”
- c# - 我可以在另一个线程上放一个取消按钮吗?
- json - 如何在后端从 JSON 读取数组
- angular - ng-bootstrap 下拉位置不适用于 Angular 7