元素类型无效:期望是字符串或类/函数,但得到的是:对象

Element type is invalid: expected a string or a class/function but got: object

本文关键字:对象 函数 无效 类型 期望 字符串 元素      更新时间:2023-09-26

我试图共享相同的代码库,所以我在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>
    );
  }
}

你需要改变你导入React的方式。

查看其他答案的详细信息:https://stackoverflow.com/a/37415044/158525

我同意你需要修改React的导入,但是它看起来并没有消除错误。根据我的测试,看起来模块绑定器在应用程序的根目录下有一个艰难的index.js。如果你将该导入更改为:

import MYCLASS from './index.js';

,它将正常工作。其他任何(./, ./index)都将导致该错误。

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