I'm working with react-native and I'm facing a problem with navigators.
The code:
App.js
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import loginScreen from './src/components/loginView';
import registerScreen from './src/components/registerView';
const Appmemes = StackNavigator({
Login: {screen: loginScreen},
Register: {screen: registerScreen}
});
export default Appmemes;
AppRegistry.registerComponent('Appmemes', () => Appmemes);
loginView.js works correctly:
.
.
.
<View style={styles.formContainer}>
<LoginForm/>
</View>
.
.
.
LoginForm.js
import React, { Component } from 'react'
import { StyleSheet, TextInput, Text, View, TouchableOpacity, AppRegistry} from 'react-native'
import { StackNavigator} from 'react-navigation';
export default class LoginForm extends Component{
render() {
return(
<View style={styles.container}>
<TouchableOpacity style={styles.buttonContainer1}>
<Text style={styles.buttonText}>Entrar</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.buttonContainer2} onPress={() => this.props.navigation.navigate('Login')}>
<Text style={styles.buttonText}>Registrarse</Text>
</TouchableOpacity>
</View>
);
}
}
AppRegistry.registerComponent('LoginForm', () => LoginForm);
const styles = StyleSheet.create({...]);
});
The error is:
undefined is not an object (evaluating _this2.props.navigation.navigate)
The error is in OnPress()
in LoginForm.js
onPress={() => this.props.navigation.navigate('Login')
What could be the cause for this error?
Simply.
<LoginForm navigation={this.props.navigation} />
The error is occurring because the
LoginFrom
isn't getting loaded directly using theStackNavigator
and only those components that are directly loaded by the StackNavigator get thenavigation
prop (likeloginScreen
in your case). Any nested components insideloginScreen
won't receive thenavigation
prop automatically so we need to pass it explicitly toLoginForm
since it will get passed tologinScreen.
I've answered a similar question here.
Sidenote: I believe you're navigating from inside
loginScreen
tologinScreen
again usingthis.props.navigation.navigate('Login')
asLoginForm
is insideloginScreen
itself. So in this case you probably mean to navigate toRegister
. So you should writethis.props.navigation.navigate('Register')
.Plus, you also don't need
AppRegistry.registerComponent('LoginForm', () => LoginForm);
This is because you only need to register one root component withAppRegistry
. So,Appmemes
should only be registered withAppRegistry
which you're already doing.LoginForm
will automatically get mounted byloginScreen
which will, in turn, get mounted byAppmemes
.