【问题标题】:have an error when i try import my component in react native当我尝试在 react native 中导入我的组件时出现错误
【发布时间】:2017-05-29 09:06:22
【问题描述】:

当我尝试导入我的组件时出现错误。 这是错误文本: “元素类型无效,应为字符串(用于内置组件)或类/函数”。

主文件 index.android.js

从 'react' 导入 React, { Component };
从 'react-native' 导入 {
AppRegistry,
StyleSheet,
Text,
View
};
从 './components/Text2' 导入 {Text2};

类 p001_lesson 扩展组件 {
render() {
return (



);
}
}

AppRegistry.registerComponent('p001_lesson', () => p001_lesson);
second file Text2.js

<pre>
import React, {Component} from 'react';
import {
    Text,
} from 'react-native';


class Text2 extends Component {
    render() {
        return <Text>some text here</Text>
    }
}

如何修复我的导入?对不起我的英语:D

【问题讨论】:

    标签: javascript android ios reactjs react-native


    【解决方案1】:

    导出组件可以解决问题, 期望导出默认您只能使用导出

    export Text2
    

    那么你应该像这样导入它:

    import { Text2 } from './components/Text2';
    

    【讨论】:

      【解决方案2】:

      如何导出 Text2 组件?

      如果您将其导出为默认导出,如下所示:

      export default Text2
      

      那么你必须像这样导入它:

      import Text2 from './components/Text2'
      

      【讨论】:

      • 我不会写出口,你
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2021-03-30
      • 2022-11-21
      • 2021-07-08
      • 2022-11-25
      • 1970-01-01
      相关资源
      最近更新 更多