combineReducers 导致代码中断

combineReducers causes code to break

这是我发布的主题的后续内容

经过大量故障排除后,我发现此代码可以正常工作

import React from 'react';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';
import { Provider } from 'react-redux';
import DataTableReducer from './reducers/DataTableReducer';
import DimensionPickerReducer from './reducers/DimensionPickerReducer';

const loggerMiddleware = createLogger();
const store = createStore(
    DimensionPickerReducer, 
    applyMiddleware(
        thunkMiddleware, 
        loggerMiddleware
    )
);

export default store;

但是如果我用像

这样的组合减速器调用替换我的单个减速器
import React from 'react';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import createLogger from 'redux-logger';
import thunkMiddleware from 'redux-thunk';
import { Provider } from 'react-redux';
import DataTableReducer from './reducers/DataTableReducer';
import DimensionPickerReducer from './reducers/DimensionPickerReducer';

const loggerMiddleware = createLogger();
const store = createStore(
    combineReducers({
        DataTableReducer,
        DimensionPickerReducer
    }), 
    applyMiddleware(
        thunkMiddleware, 
        loggerMiddleware
    )
);

export default store;

我立即开始收到 DimensionPicker 控件的错误消息,指出未指定必需的道具。

所以 combineReducer 方法对我不起作用。

我在此处上传了一个显示问题的示例项目。

https://github.com/abhitechdojo/MovieLensReact

在完成 git 克隆后 运行 npm install

使用组合减速器,您的商店将具有如下数据结构:

{
    DimensionPickerReducer: {
        dimenisionName: '',
        pickerIsLoading: false,
        pickerError: '',
        currentAttribute: '',
        attributeList: []
    },
    DataTableReducer: {
        tableData: [],
        tableIsLoading:false,
        tableError: ''
    }
}

所以你应该调整你的容器以与组合商店一起工作。例如在 DimensionPickerContainer.js 你应该改变 mapStateToProps 功能:

const mapStateToProps = (state) => {
    return {
        attributeList : state.DimensionPickerReducer.attributeList,
        currentAttribute : state.DimensionPickerReducer.currentAttribute
    }
}

你也可以在 store 中命名你的 reduce,这样它们在数据结构上就不会难看。例如。 combineReducers({ dimensionPicker: DimensionPickerReducer, dataTable: DataTableReducer})