创建可以在JS类中重用的方法/函数。

时间:2017-04-20 06:44:49

标签: javascript reactjs

我收到了React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

的错误

我想在一个类中有一个公共方法,因为我想在其他方法中重用该函数,我的代码下面有什么问题?

componentDidMount() {
    this.getCurrentMonthData('2017-10-1', '2017-12-1'); 
}
getCurrentMonthData = (from, to) => {
    //do something
}
OtherFunc = () => {
   this.getCurrentMonthData('2017-10-1', '2017-12-1');  
}

1 个答案:

答案 0 :(得分:0)

import {Your_Component_Name} from '../components/xyz.js';

VS

import Your_Component_Name from '../components/xyz.js';

在我的情况下,第二个工作,而第一个导致错误。

尝试像这样导入你的组件。