1
votes

My react-native application uses react-redux and react-navigation.

App.js imports Main component. Inside Main, react-navigation is imported and all the navigators are defined. However, I still get TypeError: undefined is not an object (evaluating 'that.navigation.navigate')] on that.navigation.navigate('Auth') in Main.

I have also tried navigation.navigate('Auth') but got Can't find variable: navigation. Is it a bad practice to import react-navigation and use it within the same file? I'd like to use navigation in Main to avoid calling firebase.auth().onAuthStateChanged in every screen.

App.js

...

const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));

class App extends React.Component {
  render() {
    return (
      <Provider store={store}>
        <Main />
      </Provider>
    );
  }
}

export default App;

Main.js

import React from 'react';
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';

...

const MainStack = createSwitchNavigator(
  {
    Home: HomeStack,
    Auth: AuthStack
  },
  {
    initialRouteName: 'Home'
  }
);

const AppContainer = createAppContainer(MainStack);

class Main extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount = () => {
    var that = this;
    firebase.auth().onAuthStateChanged(async function(user) {
        if (user) {
            await that.props.getUserProfile(user.uid);
        } else {
            that.navigation.navigate('Auth');
        }
    })
  }

  render() {
    return (
      <AppContainer />
    );
  }
}

const mapStateToProps = state => {
  return {
      user: state.auth.user
  }
}

export default connect(mapStateToProps, {getUserProfile})(Main);
2

2 Answers

0
votes

You for got props.

Change that.navigation.navigate('Auth'); to that.props.navigation.navigate('Auth');

0
votes

I'm not sure because I never used react navigation But it seems like only AppContainer and his children are wrapped and not Main Component, that's should be why navigation is undefined here. You should re-think your component architecture order


Form https://facebook.github.io/react-native/docs/navigation

Each screen component can set navigation options such as the header title. It can use action creators on the navigation prop to link to other screens

Your Main Component is not a screen, you cannot use navigation in it.

You can do something like that :

const AppContainer = isAuthentified => createAppContainer(createStackNavigator(
 {
  Home: HomeStack,
  Auth: AuthStack
 },
 {initialRouteName: !!isAuthentified ? 'Home' : 'Auth'}
)); 


class Main extends React.Component {
  constructor(props) {
   super(props);
   state = {loading: false};
  }

  componentDidMount = () => {
   let tmpuser = false; 
   firebase.auth().onAuthStateChanged(async function(user) {
    if (user) {
        tmpuser = await that.props.getUserProfile(user.uid);
    }});
   this.setState({user: tmpuser, loading: false})
  }

  render() {
   if (!this.state.loading) 
    return (
     <div>{AppContainer(this.state.user)}</div>
    );
   else return (<div>loading</div>);
  }
}

//Can be not perfect, done on smartphone