如何将模块导入jsx?

时间:2017-02-13 00:58:47

标签: javascript reactjs module jsx

使用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

1 个答案:

答案 0 :(得分:0)

首先,import {sayHello} from ...应为import sayHello from ...。其次,仅仅因为你犯了第一个错误,请确保使用Arrow而不是import {Arrow} from ...导入import Arrow from ...组件,因为前者有效且后者为我产生了一些错误,包括你提到的那个。