2017-02-13 6 views
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 

答えて

0

まず、 import {sayHello} from ...import sayHello from ...である必要があります。第二に、最初の間違いをしただけで、コンポーネントをimport Arrow from ...の代わりにimport {Arrow} from ...の代わりにインポートしていることを確認してください。

+0

それは働いていましたが、私はなぜsayHelloではなくArrowと一緒に{}を使用する必要がありますか? –

+0

'export class Arrow ...'では、 'import {Arrow} ...'を使う必要がありますが、 'export default class Arrow ...'では 'import Arrow ...'を使う必要があります。前者の場合、ファイル内に複数の 'export ...'ステートメントが存在する可能性があります。そのため、importステートメントは、変数の潜在的なリストを中括弧で囲んで複数の変数をインポートすることができなければなりません。 'export default ... 'を使うと、そのファイルからのエクスポートは1つしかできないので、importステートメントは自分自身が' import myVariable ...'という1つの変数をインポートしていることを確信できます。 –

+0

すばらしい説明ありがとう –

関連する問題