简体   繁体   English

如果用户在使用 react-navigation 打开应用程序时未登录,则显示登录屏幕

[英]Show login screen if user is not logged in when opening the app with react-navigation

I am trying to create an app which has a main tab navigator, but if the user is not logged in, he/she should first see a login screen (however, I still want the user to get the option to skip login).我正在尝试创建一个具有主选项卡导航器的应用程序,但如果用户未登录,他/她应该首先看到一个登录屏幕(但是,我仍然希望用户能够选择跳过登录)。

Also, if it's the first time the user opens the app, I want to show an onboarding/welcome screen where I explain the app.此外,如果这是用户第一次打开应用程序,我想显示一个介绍应用程序的入职/欢迎屏幕。

Right now I am persisting my redux state with redux-persist , so when I open the app (ie without having it opened in the background), I want the user to see the Login screen if he/she is not logged in and the Main screen if the user is already logged in.现在我正在使用redux-persist我的 redux 状态,所以当我打开应用程序(即没有在后台打开它)时,我希望用户看到Login屏幕,如果他/她没有登录和Main如果用户已经登录,屏幕。

I have understood that I should have my main tab navigator nested in a stack navigator, such that the stack navigator has screens WelcomeScreen , LoginScreen and Main .我知道我应该将主选项卡导航器嵌套在堆栈导航器中,以便堆栈导航器具有屏幕WelcomeScreenLoginScreenMain

But how do I check if the user is logged in when the app is first opened and then redirect to the Login screen?但是如何在首次打开应用程序时检查用户是否已登录,然后重定向到Login屏幕? I have looked at https://github.com/react-community/react-navigation/blob/master/examples/ReduxExample/src/reducers/index.js , but it's not very intuitive what is meant by firstAction , tempNavState , and secondAction .我看过https://github.com/react-community/react-navigation/blob/master/examples/ReduxExample/src/reducers/index.js ,但它不是很直观的firstActiontempNavStatesecondAction

Do I need to create a middleware which checks if the user is logged in?我是否需要创建一个中间件来检查用户是否已登录?

In the root container check for login state if the user is loggedin or not and navigate accordingly.在根容器中检查登录状态是否用户已登录并相应地导航。 See this post Login example don't use async storage as he used in the post instead use redux state to determine user is logged in. Another trick which is user friendly is to use react-native-splash-screen and don't hide the splash screen using this package, wait for 2 seconds, so that redux-persist action is dispatched which restores the state and you can navigate during this time period.请参阅此帖子登录示例不要使用他在帖子中使用的异步存储,而是使用 redux 状态来确定用户已登录。另一个用户友好的技巧是使用react-native-splash-screen并且不要隐藏使用此包的启动画面,等待 2 秒,以便调度 redux-persist 操作,恢复状态,您可以在此时间段内导航。 There will be no flickering using this method.使用这种方法不会有闪烁。

class AppWithNavigationState extends Component { 
    componentDidMount () { 
       setTimeout(()=>{ 
             if(this.props.loggedIn)
               { NavigationActions.navigate({ routeName: 'Main' }), } 

               setTimeout(SplashScreen.hide, 500)
     })
   }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM