元素类型无效:期望字符串或类/函数但得到:object

时间:2022-09-04 17:03:47

I'm trying to share the same codebase, so I wrote my code in a index.js file, and I've put the same code in the index.ios.js and index.android.js file.
This is the code in index.ios.js:

我正在尝试共享相同的代码库,所以我在index.js文件中编写了代码,并在index.ios.js和index.android.js文件中添加了相同的代码。这是index.ios.js中的代码:

'use strict';

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

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

This is some of the code in index.js:

这是index.js中的一些代码:

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

2 个解决方案

#1


0  

I agree you'll need to modify that import of React however it doesn't look like that removes the error. Based on my testing it looks like the module bundler is having a tough time with the index.js in the root of the app. If you change that import to:

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

import MYCLASS from './index.js';

从'./index.js'导入MYCLASS;

then it will work fine. Anything else (./, ./index) will result in that error.

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

I would recommend that you rename that index.js to something else like App.js Main.js etc and update the import statement accordingly. Use index.js when you want to expose resources under an organized namespace (directory).

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

#2


2  

You need to change how you import React.

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

See this other answer for the details: https://*.com/a/37415044/158525

有关详细信息,请参阅此其他答案:https://*.com/a/37415044/158525

#1


0  

I agree you'll need to modify that import of React however it doesn't look like that removes the error. Based on my testing it looks like the module bundler is having a tough time with the index.js in the root of the app. If you change that import to:

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

import MYCLASS from './index.js';

从'./index.js'导入MYCLASS;

then it will work fine. Anything else (./, ./index) will result in that error.

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

I would recommend that you rename that index.js to something else like App.js Main.js etc and update the import statement accordingly. Use index.js when you want to expose resources under an organized namespace (directory).

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

#2


2  

You need to change how you import React.

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

See this other answer for the details: https://*.com/a/37415044/158525

有关详细信息,请参阅此其他答案:https://*.com/a/37415044/158525