使用jsx / react并尝试从另一个文件导入函数。这是我的jsx:
import React from 'react';
import {sayHello} from './stuff'
export class Arrow extends React.Component {
cb = () => { // the class property is initialized with an arrow function that binds this to the class
console.log('testing=cb clicked')
}
render() {
console.log('testing=sayHello()',sayHello() )
return (
<button onClick={ this.cb }>Click</button>
);
}
}
尝试导入如下所示的sayHello:
const sayHello = () => 'say hello'
export default sayHello
当我运行jsx时,出现以下错误:
Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in
首先import {sayHello} from ...
应该是import sayHello from ...
。其次,仅仅因为您犯了第一个错误,请确保您Arrow
使用import {Arrow} from ...
而不是来导入组件import Arrow from ...
,因为前者的工作原理和后者对我来说会产生一些错误,包括您提到的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句