[英]React Native props.navigation.navigate is not an object
I'm doing the navigation between components on my React Native
App, and so far everything was working relatively ok until I got stuck with this error.我正在我的
React Native
应用程序上的组件之间进行导航,到目前为止一切都相对正常,直到我遇到这个错误。
This is the last component I want to navigate but I don't know why I can't do it, every other I followed the same structure for every component and they work, but this is the only one that doesn't.这是我想要导航的最后一个组件,但我不知道为什么我不能这样做,每个其他组件我都遵循相同的结构并且它们可以工作,但这是唯一没有的。
import React from 'react';
import {
StyleSheet,
View,
TouchableHighlight,
Button
} from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
const Icono = (props) => {
return(
<TouchableHighlight
underlayColor={'transparent'}
style={styles.icono}
onPress={() => props.navigation.navigate('Login')}>
<View>
<Icon name="video-call" color="#7796ff" size={35} />
</View>
</TouchableHighlight>
);
}
const styles = StyleSheet.create({
icono: {
paddingRight: 10,
paddingLeft: 10,
paddingTop: 17,
textAlign: 'center',
},
});
export default Icono;
This is the code I'm using to achieve the navigation.这是我用来实现导航的代码。
import React, {useState} from 'react';
import {View, StyleSheet} from 'react-native';
import {Text} from 'react-native-paper';
import {TextInput} from 'react-native-paper';
import AsyncStorage from '@react-native-community/async-storage';
import {Button} from 'react-native-paper';
import {createStackNavigator} from '@react-navigation/stack';
const Stack = createStackNavigator();
const LoginScreen = (props) => {
const [userId, setUserId] = useState('');
const [loading, setLoading] = useState(false);
const onLogin = async () => {
setLoading(true);
try {
await AsyncStorage.setItem('userId', userId);
setLoading(false);
props.navigation.navigate('Call');
} catch (err) {
console.log('Error', err);
setLoading(false);
}
};
return (
<View style={styles.root}>
<View style={styles.content}>
<Text style={styles.heading}>Ingresa tu ID</Text>
<TextInput
label="id"
onChangeText={text => setUserId(text)}
mode="outlined"
style={styles.input}
/>
<Button
mode="contained"
onPress={onLogin()}
loading={loading}
style={styles.btn}
contentStyle={styles.btnContent}
disabled={userId.length === 0}>
Conectar al servicio de video
</Button>
<Button
mode="contained"
onPress={props.navigation.navigate('Contacto')}
loading={loading}
style={styles.btn}
contentStyle={styles.btnContent}
disabled={userId.length === 0}>
Salir
</Button>
</View>
</View>
);
}
const styles = StyleSheet.create({
root: {
backgroundColor: '#fff',
flex: 1,
justifyContent: 'center',
},
content: {
paddingHorizontal: 20,
justifyContent: 'center',
},
heading: {
fontSize: 18,
marginBottom: 10,
fontWeight: '600',
},
input: {
height: 60,
marginBottom: 10,
},
btn: {
height: 60,
alignItems: 'stretch',
justifyContent: 'center',
fontSize: 18,
},
btnContent: {
alignItems: 'center',
justifyContent: 'center',
height: 60,
},
});
export default LoginScreen;
This is the code I want to navigate to.这是我要导航到的代码。 It's worth to mention that I'm using
react-navigation
, and all my components are wrapped in the <NavigationContainer>
component, but I don't understand why all the other component the navigation works and this one doesn't allow me to do it.值得一提的是,我使用的是
react-navigation
,我的所有组件都包含在<NavigationContainer>
组件中,但我不明白为什么导航的所有其他组件都有效,而这个组件不允许我这样做。
From your code the is a separate component which is not part of the navigation stack从您的代码中,它是一个单独的组件,不属于导航堆栈的一部分
You can solve this in two ways.您可以通过两种方式解决此问题。
Pass the navigation as a prop from parent which is part of the stack将导航作为道具从属于堆栈的父级传递
<Icono navigation={this.props.navigation} />
Use the useNavigation hook inside icono and call it from there.在 icono 中使用useNavigation挂钩并从那里调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.