如何从基于react redux的节点模块导出reducer

时间:2017-01-24 11:11:54

标签: javascript reactjs npm react-redux

我有一个需要从外部节点模块导入组件的项目,项目和组件都使用React和Redux,我希望它们共享同一个商店。

Heres是节点模块组件的reducer的示例:

export default function search(state = INITIAL_STATE, action) {
switch (action.type) {
    case SET_SEARCH_STRING:
        return state.set("searchString", action.searchString);
    case SET_SEARCH_TYPE:
        return state.set("searchType", action.searchType);

这是在节点模块中导出组件:

var SearchReducer = require("./WebContent/js/reducers/search");
var CollectionsReducer = require("./WebContent/js/reducers/collections");

exports.SearchReducer = SearchReducer;
exports.CollectionsReducer = CollectionsReducer;

这是主项目中的导入

import content from "./content"
    import SearchReducer from "search-ui";
    import CollectionsReducer from "search-ui";

    const REDUCERS = {content, SearchReducer, CollectionsReducer};
    export default combineReducers(REDUCERS);

当我尝试加载页面时,我收到类型错误

Uncaught TypeError:reducer不是函数 - combineReducers.js:37

现在,如果我提供节点模块reducers的直接路径 -

import collections from "../../../node_modules/search-ui/WebContent/js/reducers/collections";

它工作正常,所以我的问题是,我在导入和导出方面做错了什么?

1 个答案:

答案 0 :(得分:2)

由于它不是默认导出,请尝试此操作。

import {SearchReducer, CollectionsReducer} from 'search-ui'

或者,您可以提供完整路径,但不需要将其设为相对

import SearchReducer from 'search-ui/WebContent/js/reducers/search'
import CollectionsReducer from 'search-ui/WebContent/js/reducers/collections