1

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?

6
  • 1
    Este es el sitio en inglés, formula la pregunta en inglés o puedes pasarte al sitio español Commented Sep 13, 2017 at 16:47
  • How are we supposed understand this? Commented Sep 13, 2017 at 16:48
  • Didn't understand but I bet you are looking for THIS Commented Sep 13, 2017 at 16:52
  • Thank you for your answers! ...Gerardo muchas gracias por la aclaración, lamento mi equivocación, saludos Commented Sep 13, 2017 at 16:58
  • Don't register the login screen, just default export it Commented Sep 13, 2017 at 17:09

1 Answer 1

5

Simply. <LoginForm navigation={this.props.navigation} />

The error is occurring because the LoginFrom isn't getting loaded directly using the StackNavigator and only those components that are directly loaded by the StackNavigator get the navigation prop (like loginScreen in your case). Any nested components inside loginScreen won't receive the navigation prop automatically so we need to pass it explicitly to LoginForm since it will get passed to loginScreen.

I've answered a similar question here.

Sidenote: I believe you're navigating from inside loginScreen to loginScreen again using this.props.navigation.navigate('Login') as LoginForm is inside loginScreen itself. So in this case you probably mean to navigate to Register. So you should write this.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 with AppRegistry. So, Appmemes should only be registered with AppRegistry which you're already doing. LoginForm will automatically get mounted by loginScreen which will, in turn, get mounted by Appmemes.

Sign up to request clarification or add additional context in comments.

6 Comments

Mustan bhai .. How do I use your logic in my code below: <TouchableOpacity onPress = {() => this.props.navigation.navigate('NewPage',{id:'5'})}> <FlexCardComp mainId='5' val1='501' val2='502' /> </TouchableOpacity>
Pass down navigation prop to all the components you're using inside your screens that need to do this.props.navigation. Like I've shown for the LoginForm component in your case.
So will it be <LoginForm navigation={this.props.navigation.navigate('newPage,{id:'5'}')} /> ?
Try it like this. <LoginForm navigation={this.props.navigation} />
@JpersaudCodezit Please upvote the answer if this worked for you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.