Reputation: 793
I have created a simple app using create-react-native-app and trying to implement react-navigation in it.
The structure of the app is very simple. In the beginning, the app will load the welcome screen where the user can decide to sign-up or login if already logged then the user will be directly directed to the main home screen.
Looking through the official documentation I noticed that the use of Redux is not recommended and also there is the little ref of how to implement redux with react navigation if none.
Does anyone know how to manage the state of the navigation without Redux without getting mad?
As per official docs :
withNavigation is a higher order component which passes the navigation prop into a wrapped Component. It's useful when you cannot pass the navigation prop into the component directly, or don't want to pass it in case of a deeply nested child.
Therefore using this component is possible to have access to the props of any component.
Upvotes: 0
Views: 1232
Reputation: 4869
Check user token in AuthLoadingScreen (welcome screen in your case).
And diverge to SignUp
screen or Home
depending on user token.
For example...
WelcomeScreen(AuthLoading)
, Auth(SignUp, SignIn)
and Home( and others screen)
through createStackNavigator
.App.js
import { createSwitchNavigator, createStackNavigator } from 'react-navigation';
// Implementation of HomeScreen, OtherScreen, SignInScreen, AuthLoadingScreen
// goes here.
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: AppStack,
Auth: AuthStack,
},
{
initialRouteName: 'AuthLoading',
}
);
constructor
of AuthLoadingScreen
Class.AuthLoadingScreen.js
import React from 'react';
import {
ActivityIndicator,
AsyncStorage,
StatusBar,
StyleSheet,
View,
} from 'react-native';
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
// Fetch the token from storage then navigate to our appropriate place
_bootstrapAsync = async () => {
const userToken = await AsyncStorage.getItem('userToken');
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
this.props.navigation.navigate(userToken ? 'App' : 'Auth');
};
// Render any loading content that you like here
render() {
return (
<View style={styles.container}>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
Important thing is how to wrap screens in navigation as stack, drawer and tap.
And you can control stack various ways as
this.props.navigation.navigate('yourscreen')
this.props.navigation.goBack()
Especially, there are more control while screen is included in stack.
this.props.navigation.popToTop()
this.props.navigation.replace(yourscreen')
Ref: https://reactnavigation.org/docs/en/auth-flow.html
Upvotes: 1