New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change initial route after logging in #1878
Comments
You can use AsyncStorage persist the user as logged in and then check on component mount if the user is logged in. If true show Homepage instead of LoginPage |
If you use @jqn's approach, I'd advise using some sort of splash screen component to do the check for you. That way, you can render the splash screen until the data is loaded from AsyncStorage. If you don't use some sort of a temporary splash screen, the user will see the login screen until AsyncStorage has loaded the cached data. |
So right now I am not doing conditional rendering, instead I am nesting my routers something like this: `
So should I remove the current structure? |
Better experience is check whether logged in in root component,after that set initialRouteName with wanted component or screen. |
Can you give me an example as to how initialRouteName works? |
@Raktima I'd create a splash screen component something like so: import LogIn from './LogInNavigator';
import Home from './HomeNavigator';
class SplashScreen extends React.Component {
constructor(props) {
super(props);
this.state ={ isLoggedIn: false };
}
componentDidMount() {
AsyncStorage.getItem('loggedInStatus',
(value) => {
this.setState({ loggedInStatus: value });
});
}
render() {
if (this.state.loggedInStatus === 'loggedIn') {
return <Home/>
}
else if (this.state.loggedInStatus === 'loggedOut') {
return <LogIn screenProps={{ isLoggedIn: () => this.setState({ loggedInStatus: 'loggedIn' }) }}/>
}
return <SplashScreen/>
}
} Inside your class LogIn extends React.Component {
constructor(props) {
super(props);
}
logInUser() {
// logic to log in user through your server/API/whatever
this.props.screenProps.isLoggedIn(); // sets state in parent component which will now update and render Home
// set logged in status in AsyncStorage
}
render() {
return // login screen stuff
}
} Using this approach, I think LogIn really doesn't have to be a navigator. |
@matthamil thanks for the post, helped me solve my problem! |
My Full solution |
So basically redirecting user to the Home screen in nested navigator scenario, I did this little trick. As react-navigation does not provide a way to set the Here is our Navigation file with Redux integration, which will show you the nesting of various navigators
Now we have done a little bit tweak in the first App.js file which will run just after botting the React native application from index.ios.js/index.android.js/index.js file. We just set a state
In |
Hi can you help me here... /* @flow */ import React from "react"; import { Platform } from "react-native"; import Artboard from "./src/artboard/Artboard"; import getTheme from '../native-base-theme/components'; import Icon from 'react-native-fa-icons'; import commonColor from '../native-base-theme/variables/commonColor'; const AppNavigator = StackNavigator(
}, { export default () => ; how to change initialRouteName if logged in right now my initialRouteName Artboard if logged in then i want in place of Artboard it will go to home screen... please help |
Artboard: { screen: Artboard} //for Auth check,user is logged in or not. 2.your App navigator which initial screen is Home. const AppNavigator = StackNavigator( Home: { screen: Home}, { Now we have done ,thanks. |
Well explained authentication flow exemple in https://reactnavigation.org/docs/en/auth-flow.html |
Through This Approach
IN SPLASH SCREEN PUT A CHECK IN UseEffect() HOOK
|
Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro. |
I'm new in react native and currently developing a react-native app that require login.After successful login, the view change into Homepage. The problem is after i close and re-open the app, it shows me LoginPage again. Normally when you close some app that require login and re-open it, it will redirect to home page if you're already logged in. but in my case, it always show the LoginPage first.
How to change initial route to HomePage if the user has already logged after closing and re-open the app?
The text was updated successfully, but these errors were encountered: