0
jsx/reactを使用して、別のファイルから関数をインポートしようとしています。このように見えるのsayHelloをインポートしようとしモジュールを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>
);
}
}
::これは私のJSXである私はJSXを実行すると
const sayHello =() => 'say hello'
export default sayHello
私はこのエラーを取得:すべての
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
それは働いていましたが、私はなぜsayHelloではなくArrowと一緒に{}を使用する必要がありますか? –
'export class Arrow ...'では、 'import {Arrow} ...'を使う必要がありますが、 'export default class Arrow ...'では 'import Arrow ...'を使う必要があります。前者の場合、ファイル内に複数の 'export ...'ステートメントが存在する可能性があります。そのため、importステートメントは、変数の潜在的なリストを中括弧で囲んで複数の変数をインポートすることができなければなりません。 'export default ... 'を使うと、そのファイルからのエクスポートは1つしかできないので、importステートメントは自分自身が' import myVariable ...'という1つの変数をインポートしていることを確信できます。 –
すばらしい説明ありがとう –