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);