javascript - Store 没有带有 mergeReducer 的有效 reducer

标签 javascript node.js reactjs ecmascript-6 redux

我一直在尝试弄清楚如何在服务器端按照 official document 使用 combineReducers .

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

列表 reducer :

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(我需要将一些初始数据传递给列表缩减器以便动态添加或删除项目):

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

}

但是客户端页面加载时出现的 reducer 故障错误是什么?

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

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

最佳答案

问题在于您实际上没有将函数传递给 combineReducers 函数。当您执行 letlistingStore = 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);
...

关于javascript - Store 没有带有 mergeReducer 的有效 reducer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38400731/

相关文章:

javascript - 我如何减少图表 js 圆环图的边框宽度

javascript - 代码 return val + '' === numVal + '' 的用途是什么?

javascript - 在提供 css 属性时,内联未在 Material ui 中定义

javascript - 如何合并和匹配 2 个单独的对象?

javascript - Angular 页面刷新中发生了什么为什么我的登录用户无法识别

node.js - NodeJs如何写入文件

node.js - 在 Google App Engine 上设置 AppRTC (WebRTC)

reactjs - 使用 React Final Form 将自定义 prop 添加到 RadioGroup 字段

javascript - 在 BeginContact 事件中设置新的 body 位置

javascript - 在 React Component 的状态中渲染一个对象的属性,在数组中