首页 文章

React Native ReferenceError无法找到变量

提问于
浏览
3

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

代码:

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);

错误:

ReferenceError:无法找到变量:MyAppHeader此错误位于:RCTView(在View.js:113)的Test2中(在renderApplication.js:35处)在RCTView中的View(at AppContainer.js:102)中AppContainer中的View(at AppContainer.js:122)中的View.js:113)(在renderApplication.js:34)

1 回答

  • 3

    正如德里克所说,

    你从未定义过MyAppHeader,因此你会收到错误 .

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

    否则,您需要定义MyAppHeader组件才能使其正常工作 .

    很明显发布React Components Components and Props - React

    希望它会有所帮助 .

相关问题