当我尝试在 React Native 中导入我的组件时出错 [英] have an error when i try import my component in react native

查看:29
本文介绍了当我尝试在 React Native 中导入我的组件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试导入我的组件时出现错误.这是错误文本:元素类型无效,预期为字符串(对于内置组件)或类/函数".

主文件 index.android.js

<前>import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';

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

class p001_lesson extends Component {
render() {
return (

<视图>
<Text2/>
</View>
);
}
}

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

第二个文件 Text2.js<预>从反应"导入反应,{组件};进口 {文本,来自'反应原生';类 Text2 扩展组件 {使成为() {返回  一些文本在这里}}

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

解决方案

如何导出 Text2 组件?

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

导出默认 Text2

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

从'./components/Text2'导入Text2

I got an error when i try import my component. here is text of error : "element type is invalid expected a string (for built-in components) or a class/function".

Main file index.android.js


import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';

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

class p001_lesson extends Component {
render() {
return (
<View>
<Text2/>
</View>
);
}
}

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>
    }
}

How i can fix my import? Sorry for my English :D

解决方案

How do you export your Text2 component?

If you exported it as a default export, like this:

export default Text2

then you have to import it like this:

import Text2 from './components/Text2'

这篇关于当我尝试在 React Native 中导入我的组件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆