报错如下:
解决:在render函数内部添加:
const { navigation } = this.props;
相关代码如下:
Page1页面代码:
import React, { Component } from 'react';
import { Text, Button, View, StyleSheet } from 'react-native';
export default class Page1 extends Component {
render() {
const { navigation } = this.props;//不加这句会报错:Can't find variable: navigation
return (
<View style={styles.container}>
<Text>Welcome to Page1</Text>
<Button
title = {'Go Back'}
onPress = {()=>{
navigation.goBack();
}}
/>
<Button
title = {'跳转到Page4'}
onPress = {()=>{
navigation.navigate('Page4');
}}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex:1,
}
})