简体   繁体   English

React Native 找不到变量:导航

[英]React Native can't find variable: navigate

I am doing stack navigation but I can't seem to be able to navigate I will get this error "Can't find variable: navigate" Here is the screenshot of my android emulator我正在执行堆栈导航,但似乎无法导航我会收到此错误“找不到变量:导航”这是我的 android 模拟器的屏幕截图

This is my App class(main)这是我的应用程序类(主要)

export default class App extends Component {
render() {
return (
  <View style={styles.container}>
    <Header/>
    <AppNavigator/>
  </View>
);
}
}

const AppNavigator = StackNavigator({
Cluster1: { 
  screen: Cluster1,
  },
Play: { 
  screen: Play,
  },
});

This is my Cluster1 class这是我的 Cluster1 类

export default class Cluster1 extends Component{
render(){
    return(
        <View>
            <SectionList          
             
             renderSectionHeader={({ section }) => {
                return (<SectionHeader section={section} />);
            }}
             sections={ClusterData}
             keyExtractor={(item, index) => item.name}
        >
        </SectionList>
        </View>
    );
  }
} 
 class SectionHeader extends Component {
    render() {
        return (
            <View style={styles.header}>  
                <Text style={styles.headertext}>
                {this.props.section.title}       
                </Text>
                <TouchableOpacity onPress={() => { navigate("Play");}}>
                <Text style ={styles.Play}>Play
                </Text>
                </TouchableOpacity>
            </View>
        );
    }
    }

navigation object only exist in the screen component.导航对象只存在于屏幕组件中。 (not exist in the nested components). (不存在于嵌套组件中)。 you can pass it into the nested component using props您可以使用 props 将其传递到嵌套组件中

export default class Cluster1 extends Component{
render(){
    return(
        <View>
            <SectionList          

             renderSectionHeader={({ section }) => {
                return (<SectionHeader navigation={this.props.navigation} section={section} />);
            }}
             sections={ClusterData}
             keyExtractor={(item, index) => item.name}
        >
        </SectionList>
        </View>
    );
  }
} 
class SectionHeader extends Component {
    render() {
        return (
            <View style={styles.header}>  
                <Text style={styles.headertext}>
                {this.props.section.title}       
                </Text>
                <TouchableOpacity onPress={() => { this.props.navigation.navigate("Play");}}>
                <Text style ={styles.Play}>Play
                </Text>
                </TouchableOpacity>
            </View>
        );
    }
}

Include on your SectionHeader the this.props.navigation something like this:在你的 SectionHeader 中包含this.props.navigation是这样的:

<SectionHeader navigation={this.props.navigation}/>

because the props.navigation are by default on your parent component因为props.navigation默认在你的父组件上

and on SectionHeader component you will access to navition like:在 SectionHeader 组件上,您将访问导航,如:

..
goToSignUp() {
   this.props.navigation.navigate('SignUp');
}
..

For me also was confusing before.对我来说之前也很困惑。 Cheers!干杯!

You can use this rather than navigate :您可以使用它而不是导航:

this.props.navigation.navigate('Play')

Hope this is helpful.希望这是有帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM