Search code examples
reactjsreact-nativeex-navigation

React native ex-navigation changing routers


I have a problem in switching pages with react-native ex-navigation: Error: undefined is not an object (evaluating 'this.props.navigator'

this is the code:

import React, { Component } from 'react';
import {AppRegistry, StyleSheet, Text, View, Navigator, Button } from 'react-native';
import {createRouter, NavigationProvider, StackNavigation, TabNavigation,TabNavigationItem as TabItem,}from '@exponent/ex-navigation';
import * as firebase from "firebase";

firebase.initializeApp(config);

import Login from 'auth/firstPage'
var Register = require('auth/register')
var Tab = require('./src/components/tab/mainTab');

const Router = createRouter(() => ({
  login: () => Login,
  register: () => Register,
  tab: () => Tab,
}));

export default class main extends Component {

  componentWillMount(){
    this.setState({ name:'login'});
  } 

  constructor(props) {
    super(props);
    this.state = {
      name: 'tab'
    };
  }

  render(){
    return(
     <NavigationProvider router={Router}>
       <StackNavigation initialRoute={Router.getRoute(this.state.name)} />
     </NavigationProvider>
   )
 }
}

this is the code I used to navigate: this.props.navigator.push({name:'register'})


Solution

  • Did you call this.props.navigator in another function in your new page component?

    You will need to add the following in your new page constructor which gives you access to this.props.navigator:

    constructor(props){
      super(props);
      this.FUNCTION_CALL_NAV = this.FUNCTION_CALL_NAV.bind(this);
    }
    
    FUNCTION_CALL_NAV() {
      this.navigator.push({name: 'register'});
    }