我有一个项目需要从外部节点模块导入组件,该项目和组件都使用React和Redux,我希望它们都共享同一商店。
以下是节点模块组件的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);
当我尝试加载页面时,出现类型错误
未被捕获的TypeError:减速器不是函数-CombineReducers.js:37
现在,如果我提供到节点模块减速器的直接路径-
即
import collections from "../../../node_modules/search-ui/WebContent/js/reducers/collections";
它工作正常,所以我的问题是,在导入和导出方面我做错了什么?
最佳答案
由于它不是默认导出,请尝试此操作。
import {SearchReducer, CollectionsReducer} from 'search-ui'
另外,您可以提供完整的路径,但无需使其相对
import SearchReducer from 'search-ui/WebContent/js/reducers/search'
import CollectionsReducer from 'search-ui/WebContent/js/reducers/collections
关于javascript - 如何从基于React Redux的节点模块导出reducer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41826740/