[英]Repeatedly popping "undefined is not an object (evaluating 'navigation.navigate')" after routing a particular screen
const ResolvedPlaces = ({navigation}) => { return( <View style={Styles.headerWrapper}> <TouchableOpacity navigation={navigation} onPress={()=> navigation.navigate("ProfileScreen")} > <Text style={[Styles.header,{fontWeight:"bold"}]}>Resolved </Text> <Text style={Styles.header}> places</Text> </TouchableOpacity> </View> ) }
I have created a ResolvedPlaces function and gave a option to route to the profile screen.我创建了一个 ResolvedPlaces function 并提供了一个路由到配置文件屏幕的选项。 Also I've destructured the "navigation" in all format.
我还解构了所有格式的“导航”。 But I continuously got this navgation.navigate as undefined object. the error message on the terminal
但我不断得到这个 navgation.navigate as undefined object.终端上的错误信息
If ResolvedPlaces
is not defined as a Screen
in a react-native-navigation navigator
, then the navigation prop
will not be passed automatically to the component ResolvedPlaces
.如果
ResolvedPlaces
未在react-native-navigation navigator
中定义为Screen
,则navigation prop
将不会自动传递给组件ResolvedPlaces
。
If ResolvedPlaces
is nested inside a Screen
that is defined in a Navigator
, then you can use the navigation prop
directly.如果
ResolvedPlaces
嵌套在Navigator
中定义的Screen
中,那么您可以直接使用navigation prop
。 This could look as follows.这可能如下所示。
const SomeScreen = ({navigation}) => {
const ResolvedPlaces = () => {
return(
<View style={Styles.headerWrapper}>
<TouchableOpacity onPress={()=> navigation.navigate("ProfileScreen")} >
<Text style={[Styles.header,{fontWeight:"bold"}]}>Resolved </Text>
<Text style={Styles.header}> places</Text>
</TouchableOpacity>
</View>
)
}
return (
<ResolvedPlaces />
)
}
The component SomeScreen
must be defined in a navigator, eg a Stack
.组件
SomeScreen
必须在导航器中定义,例如Stack
。
If ResolvedPlaces
is a component defined in its own file, then pass the navigation
object as a prop from the parent that is defined as a screen.如果
ResolvedPlaces
是在其自己的文件中定义的组件,则将navigation
object 作为来自定义为屏幕的父级的道具传递。
const SomeScreen = ({navigation}) => {
return <ResolvedPlaces navigation={navigation} />
}
If this is not an option than you can still use the useNavigation
hook as follows.如果这不是一个选项,那么您仍然可以按如下方式使用
useNavigation
挂钩。
const ResolvedPlaces = () => {
const navigation = useNavigation()
return (
<View style={Styles.headerWrapper}>
<TouchableOpacity onPress={()=> navigation.navigate("ProfileScreen")} >
<Text style={[Styles.header,{fontWeight:"bold"}]}>Resolved </Text>
<Text style={Styles.header}> places</Text>
</TouchableOpacity>
</View>
)
}
Remarks: I have removed the navigation prop from the TouchableOpacity
since this prop does not exist for this component.备注:我已经从
TouchableOpacity
中删除了导航道具,因为该组件不存在该道具。 The basic react-native-navigation
structure is documented here .基本的
react-native-navigation
结构在此处记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.