javascript React Native ReferenceError 找不到变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47547182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:27:29  来源:igfitidea点击:

React Native ReferenceError Can't find variable

javascriptandroidreact-native

提问by Vyshnav Girish

Have been going through the documentation in https://facebook.github.io/react-native/docs/text.html. Its not clear how to link the reference between the two classes. I am trying to use the tag instead of and it this gives no reference found error.

一直在浏览https://facebook.github.io/react-native/docs/text.html 中的文档。不清楚如何链接两个类之间的引用。我正在尝试使用标签而不是,并且它没有给出任何参考发现错误。

Code :

代码 :

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

class MyAppHeaderText extends Component {

    render() {
        return(
            <MyAppHeader>
                <Text style={{fontSize:20}}>
                    {this.props.children}
                </Text>
            </MyAppHeader>
        )
    }
}

class Test2 extends Component {

    constructor(props){
        super(props);

        this.state = {
            mainText: 'This is Bird',
            subText : 'Not dino'
        }
    }

    render() {
        return(
            <View>
                {/* <Text>

                    {this.state.mainText}
                    {this.state.subText}
                </Text> */}
                <MyAppHeaderText>
                    <MyAppHeader>
                        {this.state.mainText}
                        {this.state.subText}
                    </MyAppHeader>
                </MyAppHeaderText>
            </View>
        )
    }
}

export default MyAppHeaderText;

AppRegistry.registerComponent('AwesomeProject',() => Test2);

Error :

错误 :

ReferenceError: Can't find variable: MyAppHeader

This error is located at: in Test2 (at renderApplication.js:35) in RCTView (at View.js:113) in View (at AppContainer.js:102) in RCTView (at View.js:113) in View (at AppContainer.js:122) in AppContainer (at renderApplication.js:34)

参考错误:找不到变量:MyAppHeader

此错误位于:在 Test2 (at renderApplication.js:35) in RCTView (at View.js:113) in View (at AppContainer.js:102) in RCTView (at View.js:113) in View (at AppContainer.js:122) 在 AppContainer(在 renderApplication.js:34)

采纳答案by ChouW

As Derek mentioned,

正如德里克所说,

you have never defined MyAppHeader, therefore you will get error.

你从未定义过MyAppHeader,因此你会得到错误。

You could delete all <MyAppHeader></MyAppHeader>in your project, and it should work!

您可以删除<MyAppHeader></MyAppHeader>项目中的所有内容,它应该可以工作!

Otherwise you will need to defined MyAppHeader Component to make it works.

否则,您将需要定义 MyAppHeader 组件以使其工作。

Clearly post for React Components Components and Props - React

明确发布 React Components组件和道具 - React

Hope it will help.

希望它会有所帮助。

回答by mcturanli

That way you'il do what you want.

那样你就可以为所欲为

class Test2 extends Component {

    constructor(props){
        super(props);

        this.state = {
            mainText: 'This is Bird',
            subText : 'Not dino'
        }
    }

    render() {
        return(
            <View>
                {/* <Text>

                    {this.state.mainText}
                    {this.state.subText}
                </Text> */}
                <MyAppHeaderText>
                        {this.state.mainText}
                        {this.state.subText}
                </MyAppHeaderText>
            </View>
        )
    }
}