簡體   English   中英

當我嘗試在 React Native 中導入我的組件時出錯

[英]have an error when i try import my component in 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 (
<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>
    }
}

我如何修復我的導入? 對不起我的英語 :D

你如何導出你的 Text2 組件?

如果您將其導出為默認導出,如下所示:

export default Text2

那么你必須像這樣導入它:

import Text2 from './components/Text2'

導出您的組件將可以解決問題,期望導出默認值您只能使用導出

export Text2

然后你應該像這樣導入它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM