0
votes

I'm working with react native and right now I getting the following error when I tried to mount my loading component.

TypeError: undefined is not an object (evaluating 'this.props.navigation.navigate')

Here is my code

import React from 'react';
import SplashScreen from 'react-native-splash-screen';
import Loading from '../../components/loading/Loading';
import { connect } from 'react-redux';

class LoadingContainer extends React.Component {
   componentDidMount() {
      setTimeout(() => {
         SplashScreen.hide();
      }, 300);
      let initRoute;
      if (this.props.auth.user) {
         initRoute = 'App';
      } else {
         initRoute = 'Login';
      }
      this.props.navigation.navigate(initRoute);
   }

   render() {
      return <Loading navigation={this.props.navigation} />;
   }
}

const mapStateToProps = state => ({
   auth: state.auth,
});

export default connect(mapStateToProps, null)(LoadingContainer);

Is the first time I getting this error while using react-navigation, I'm using the version 2.11.2 of react navigation

1
please share your navigation configuration, is LoadingContainer declared in it? - Charlie
import React from 'react'; import { createSwitchNavigator } from 'react-navigation'; import App from './AppNavigator'; import LoadingContainer from '../containers/LoadingContainer/LoadingContainer'; import Login from '../containers/LoginContainer/Login'; export default createSwitchNavigator( { Loading: () => <LoadingContainer />, Login: () => <Login />, }, { initialRouteName: 'Loading', }, ); - Gabriel Gonzalez

1 Answers

0
votes

Why don't you use react-navigation v4.x?

For v2.x, your navigation configuration could be like this

import React from 'react';
import { createStackNavigator } from 'react-navigation';
import LoadingContainer from '../containers/LoadingContainer/LoadingContainer';
import Login from '../containers/LoginContainer/Login';

const AppStack = createStackNavigator(
    {
        Loading: LoadingContainer,
        Login: Login,
    },
    {
        initialRouteName: 'Loading',
    }
);

export default class App extends React.Component {
    render() {
        return <AppStack />
    }
}