TypeError: Cannot read property 'navigation' of undefined
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
TextInput,
Button
} from 'react-native';
import axios from 'axios';
export default class Login extends Component
UserLogin = () =>{
const {Username,UserPassword}=this.state;
axios({
method:"POST",
url:'http://192.168.1.102/react/user_login.php',
data:'NameUser='+this.state.UserName2+'&PasswordUser='+this.state.UserPassword2
}).then(function (response){
if(response.data.Message === "Login Succesfull"){
this.props.navigation.navigate('Profile');
}
else {
console.log(response.data);
}
})
.catch(function (error) {
console.log(error);
});
}
render() {
return (
const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', margin: 0, backgroundColor: '#eef9fe', padding: 10 }, textStyle: { fontSize: 20, color: "#000", textAlign: "center", marginBottom: 10 }, inputStyle: { height: 40, marginBottom: 8, borderWidth: 1, borderColor: "#2196F3", borderRadius: 5 } });
this.props.navigation.navigate('Profiled'); in if statement doesn't work but in onPress event is worked (onPress={() => this.props.navigation.navigate('Details')} = is work) what id wrong?
see: https://reactnavigation.org/docs/connecting-navigation-prop.html
that said, this is a question which is better suited for stackoverflow or discord. see https://reactnavigation.org/help.html for more information on how to get help.
StudioAleph Do you solve it. I have the same issue.
I solved it using this url: https://medium.com/@ashleywnj/react-navigation-cannot-read-property-navigate-of-undefined-94ff5e9d3c2a
hope it helps someone
https://reactnavigation.org/docs/en/with-navigation.html `import React from 'react'; import { Button } from 'react-native'; import { withNavigation } from 'react-navigation';
class MyBackButton extends React.Component { render() { return <Button title="Back" onPress={() => { this.props.navigation.goBack() }} />; } }
// withNavigation returns a component that wraps MyBackButton and passes in the // navigation prop export default withNavigation(MyBackButton);`