I'm very new to React Native, right now I'm trying to build a Login component, once login succeed, I'd like to redirect the component from Login screen to the main screen. I'm using React Navigation and somehow it keeps showing me the error "Cannot read property 'navigate' of undefined".
Did I do something wrong or I just missed something? Very appreciate for any kind help!
I'm using Visual Studio Code-Insiders (v1.37.0) npm -v:6.4.1 expo -V:2.21.2 react-navigation: ^3.11.0
The code very basic:
import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Avatar, Button, Icon } from 'react-native-elements'
import * as t from 'tcomb-form-native'
import Router from '../config/router';
const Form = t.form.Form;
const PWD = '2';
const UME = 'J';
class Login extends Component {
constructor(props) {
super(props);
this.state = {
login: {}
};
this.LoginModel = t.struct({
un: t.String,
pwd: t.String,
rm: t.Boolean
});
this.LoginOptions = {
fields: {
un: {
label: 'User Name',
error: 'Please type your username'
},
pwd: {
label: 'Password',
password: true,
secureTextEntry: true,
error: 'Please type the password'
},
rm: {
label: 'Remember me'
}
}
}
}
onChange(form) {
this.state.login = form;
}
onSubmit() {
console.log(this.props); // will output an empty object! but how?
const { navigate } = this.props.navigation; // Here is where the error message captured and throw out!
const value = this.refs.form.getValue();
if (value.un === UME && value.pwd === PWD) {
alert('Welcome back, Jack');
// setTimeout(() => {
// navigate('Home');
// }, 300);
}
}
render() {
return (
<View style={styles.container}>
<View style={{ alignItems: "center", marginBottom: 30 }}>
<Avatar
size="xlarge"
icon={{ name: 'apple', color: 'black', type: 'font-awesome' }}
overlayContainerStyle={{ backgroundColor: 'white' }}
onPress={() => console.log("Works!")}
activeOpacity={0.7}
containerStyle={{ marginTop: 15 }}
/>
</View>
<Form
ref="form"
type={this.LoginModel}
value={this.state.login}
options={this.LoginOptions}
onChange={(f) => this.onChange(f)}
/>
<Button
icon={
<Icon
type='font-awesome'
name="user"
size={25}
color="white"
iconStyle={styles.icon}
/>
}
title="Login"
style={styles.btn}
onPress={()=>this.onSubmit()}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 80
},
icon: {
paddingRight: 10
},
btn: {
}
});
export default Login;
UPDATE: This is the App.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import LoginScreen from './src/screens/Login'
export default function App() {
return (
<LoginScreen />
);
}
Logincomponent is properly hook in thereact-navigationstack? And please don't directly make changes of your statethis.state.login = form;, usethis.setStateinstead - Ewe Tek Min