【问题标题】:Element type is invalid: expected a string or a class/function but got: object元素类型无效:应为字符串或类/函数,但得到:对象
【发布时间】:2016-06-27 21:09:56
【问题描述】:

我正在尝试共享相同的代码库,因此我将代码写入了 index.js 文件中,并将相同的代码放入了 index.ios.jsindex.android.js 文件中。
这是index.ios.js中的代码:

'use strict';

    import React, { AppRegistry } from 'react-native';
    import MYCLASS from './index';

    AppRegistry.registerComponent('MYCLASS', () => MYCLASS);

这是index.js中的部分代码:

export default class MYCLASS extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.title}>
          my text
        </Text>
      </View>
    );
  }
}

【问题讨论】:

    标签: javascript android react-native ecmascript-6


    【解决方案1】:

    您需要更改导入 React 的方式。

    查看其他答案了解详情:https://stackoverflow.com/a/37415044/158525

    【讨论】:

      【解决方案2】:

      我同意您需要修改 React 的导入,但看起来并没有消除错误。根据我的测试,看起来模块捆绑器在应用程序根目录中使用 index.js 时遇到了困难。如果您将该导入更改为:

      import MYCLASS from './index.js';

      然后它会正常工作。其他任何东西(././index)都会导致该错误。

      我建议您将 index.js 重命名为 App.js Main.js 等其他名称,并相应地更新导入语句。当您想要在有组织的命名空间(目录)下公开资源时,请使用 index.js

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-18
        • 2020-05-23
        • 2017-06-10
        • 2020-03-11
        • 1970-01-01
        • 1970-01-01
        • 2019-11-25
        • 2020-11-11
        相关资源
        最近更新 更多