【问题标题】:Store does not have a valid reducer with combineReducer商店没有使用 combineReducer 的有效减速器
【发布时间】:2016-07-15 16:12:20
【问题描述】:

我一直试图弄清楚如何在official document之后在服务器端使用combineReducers

这是我尝试组合的两个减速器,但没有成功:

ListingReducer:

import ActionType from '../ActionType'

export default function ListingReducer ( state = Immutable.List.of(), action){
    switch(action.type) {
        case ActionType.ADD:
            return [
                ...state,
                action.item

            ];
        case ActionType.DELETE:
            return state.filter(function(cacheItem){
                return cacheItem.id !== action.item.id;
        });
        default:
            return state
    }
}

DialogShowHideReducer:

import ActionType from '../ActionType'

export default function DialogShowHideReducer ( state = false, action){
    switch(action.type) {
        case ActionType.DIALOG:
            state = action.visible?false:true;
            return state;
        default:
            return state;
    }
}

Store.js(我需要将一些初始数据传递给listing reducer,以便动态添加或删除项目):

import {createStore} from 'redux';
import { combineReducers } from 'redux';
import ListingReducer from '../reducer/ListingReducer';
import DialogReducer from '../reducer/DialogShowHideReducer';

export default function (initData){
    let listingStore = ListingReducer(initData.item,{});
    let dialogStore = DialogShowHideReducer(false,{'type':'default'});

    // !!!!!!No reducers coming out of this function!!!!!!!!!!
    let combineReducer = combineReducers({
        listing:listingStore,
        dialog:dialogStore
    });

    return createStore(combineReducer)
}

homepage_app.js

import store from './store/Store'
import CustomComponent from './custom_component';

export default class HomePage extends React.Component {

    render() {
        <Provider store={store(this.props)}>
        <CustomComponent/>
        </Provider>
    }

}

但是关于客户端页面加载的减速器失败错误是什么?

 Store does not have a valid reducer. 
 Make sure the argument passed to combineReducers 
 is an object whose values are reducers.

官方指南和我的示例之间的主要区别在于,我将初始状态传递给一些减速器,然后再将它们传递给combineReducers

【问题讨论】:

    标签: javascript node.js reactjs ecmascript-6 redux


    【解决方案1】:

    问题是您实际上没有将函数传递给 combineReducers 函数。当您执行let listingStore = ListingReducer(initData.item,{}); 之类的操作时,您正在传递减速器函数的结果。这会将listingStore 设置为等于从reducer 函数返回的状态,而不是reducer 函数本身。

    如果您需要动态地将初始状态传递给您的 reducer(即不将它们硬编码到 reducer 中),Redux 为 createStore 函数提供了一个 preloadedState 参数。

    因此,您需要执行以下操作,而不是您所做的:

    ...
    let combineReducer = combineReducers({
        listing: ListingReducer //function
        dialog: DialogShowHideReducer //function
    });
    
    let initialState = ... // your initial state here
    return createStore(combineReducer, initialState);
    ...
    

    【讨论】:

    • 如果我想将不同的数据传递给不同的子减速器。 'createStore' 如何知道数据将相应地转到哪个 sub-reducer?
    • 它以同样的方式“知道”如何将某些动作分派给某些减速器。你应该深入研究 Redux 的源代码,或者在这里阅读 Dan Abramov 的优秀教程:egghead.io/lessons/… 了解更多信息。
    猜你喜欢
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    相关资源
    最近更新 更多