17

这是我在此处发布的主题之后

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

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

npm install执行 git clone 后必须运行

4

1 回答 1

42

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

{
    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})

于 2016-02-15T06:44:15.190 回答