【问题标题】:How to import reducers from various files?如何从各种文件中导入减速器?
【发布时间】:2019-05-14 15:44:52
【问题描述】:

我正在将减速器导入到单个文件中。我将如何正确引用它。它目前一直显示为“未定义”。

这是我的目录设置: redux -> reducer.js redux -> process.js

process.js 包含一个 reducer

  export  const process = (state = initialState.process, action) => {
    switch (action.type) {
      case PROCESS_PENDING:
        return {...state, 
          pending: true
        };
        return state;
    }
  }; 

reducer.js 是我用来访问流程减速器并最终合并它的文件

import process from './process'; 

let test = process;
console.log (test) //undefined

export const allRoot = {
  process,
  values
}

我希望变量 test 的输出包含我的 reducer。 我希望 allRoot 包含我的流程缩减器。

实际结果是过程未定义。

【问题讨论】:

  • 您只在使用export default anyName 导出时使用import name from './file.js'
  • 第一个问题是importing,有提到过,第二个,这不是reducers的组合方式,redux本身就有combineReducers的特性

标签: reactjs ecmascript-6 redux import


【解决方案1】:

使用import { process } from './process',或者如果你将来有很多reducer,你可以使用import * as reducers from './process'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多