反应原生导航错误‘_this.props.navigation.navigate’
2021-12-12
914
我是 react-native 的新手。为了学习,我创建了一个示例项目。我想通过按下抽屉内的文本导航到另一个页面。所以我遇到了错误
TypeError: undefined is not an object (evaluating '_this.props.navigation.navigate')
我添加了示例代码
import React,{Component} from "react";
import { StyleSheet, Text, View ,ImageBackground,} from "react-native";
export default class DrawerContent extends Component{
render(){
return(
<View style={Styles.container}>
<ImageBackground source={require('../../assets/drawerbg.jpg')}>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Home')}>Home</Text>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Profile')}>Profile</Text>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Settings')}>Settings</Text>
</ImageBackground>
</View>
)
}
}
3个回答
DrawerNavigator 内呈现的所有组件都必须从 DrawerNavigator 继承导航道具。
DrawerNavigator 必须是所有其他导航器(Tabs 和 Stacks)的父级。
根据这些指导原则,我们可以重构代码如下:
import React, { Component } from "react";
import { StyleSheet, Text, View, ImageBackground } from "react-native";
import { createDrawerNavigator } from "@react-navigation/drawer";
import { NavigationContainer } from "@react-navigation/native";
class DrawerContent extends Component {
render() {
return (
<View style={Styles.container}>
<ImageBackground source={require("../../assets/drawerbg.jpg")}>
<Text
style={Styles.TextFiled}
onPress={() => this.props.navigation.navigate("Home")}
>
Home
</Text>
<Text
style={Styles.TextFiled}
onPress={() => this.props.navigation.navigate("Profile")}
>
Profile
</Text>
<Text
style={Styles.TextFiled}
onPress={() => this.props.navigation.navigate("Settings")}
>
Settings
</Text>
</ImageBackground>
</View>
);
}
}
const MainNavigation = () => {
return (
<NavigationContainer>
<Drawer.Navigator drawerContent={(props) => <DrawerContent {...props} />}>
{/* Other screens here */}
</Drawer.Navigator>
</NavigationContainer>
);
};
export default MainNavigation;
查看此行
<Drawer.Navigator drawerContent={(props) => <DrawerContent {...props} />}>
,了解我们如何将导航道具从抽屉传递到子组件。
Fiston Emmanuel
2021-12-12
您需要导航到一个组件以为其提供导航 prop,如果没有,请使用 withNavigation 方法导出您的组件以为您提供 prop:
import React,{Component} from "react";
import { StyleSheet, Text, View ,ImageBackground,} from "react-native";
import { withNavigation } from 'react-navigation';
class DrawerContent extends Component{
render(){
return(
<View style={Styles.container}>
<ImageBackground source={require('../../assets/drawerbg.jpg')}>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Home')}>Home</Text>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Profile')}>Profile</Text>
<Text style={Styles.TextFiled}
onPress = {()=> this.props.navigation.navigate('Settings')}>Settings</Text>
</ImageBackground>
</View>
)
}
}
export default withNavigation(DrawerContent )
或者您可以使用 useNavigation 方法,就像在 这里 中用于 React Navigation 6 一样。
Mostafa Elkaramany
2021-12-12
通过 useNavigation 解决了答案
import React,{Component} from "react";
import { StyleSheet, Text, View ,ImageBackground,} from "react-native";
import { useNavigation } from "@react-navigation/native";
const DrawerContent =() => {
const navigation = useNavigation()
return(
<View style={Styles.container}>
<Text style={Styles.TextFiled}
onPress = {()=>navigation.navigate('Home') }>Home</Text>
<Text style={Styles.TextFiled}
onPress = {()=> navigation.navigate('Profile')}>Profile</Text>
<Text style={Styles.TextFiled}
onPress = {()=> navigation.navigate('Settings')}>Settings</Text>
</View>
)
}
export default DrawerContent
Arjun_B
2021-12-12