I am new at React-Native. I just keep getting same error that "Cannot read property 'navigate' of undefined". Here is the sample code
export default class Form extends Component {
constructor(props) {
super(props);
this.loginHandler = this.loginHandler.bind(this);
this.navigate = this.props.navigation;
this.successCallback = this.successCallback.bind(this);
this.state = {
email: '',
password: '',
}
}
// TODO - Login Authentication
loginHandler() {
var loginUrl = 'someURL';
fetch(loginUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: this.state.email,
password: this.state.password
}),
})
.then(response => { return response.json(); })
.then(responseData => { return responseData; })
.then((responseData) => {
this.successCallback(responseData.token)
})
.catch((err) => { console.log(err); });
}
successCallback(token) {
if (token === undefined)
alert("Email or password is incorrect !");
else {
this.props.navigation.navigate('User', {tokenFromUser: token});
}
}
In successCallback function, if token is generated it means login credentials are correct so it should navigate to UserScreen with paremeter 'token'. Can anyone help me ?
Here is the how I call loginHandler function
<TouchableOpacity style={styles.button} onPress={this.loginHandler} >
<Image source={loginImage} style={styles.image} />
<View style={styles.SeparatorLine} />
<Text style={styles.text}>
Enter
</Text>
</TouchableOpacity>
Formregistered for navigation?Form.jsis a component try using the hoc wrapper for navigation as this or pass the navigation props from parent file