简体   繁体   English

NativeStackNavigator React Native - 身份验证

[英]NativeStackNavigator React Native - Authentication

I made a login screen for my app, and a homepage.我为我的应用程序和主页制作了一个登录屏幕。 My goal is to store if the users is logged in, and if he is, to not send him through the login page, but directly to the mainpage.我的目标是存储用户是否已登录,如果已登录,则不通过登录页面发送给他,而是直接发送到主页。 I found a way online, using this method shown in the code.我在网上找到了一种方法,使用代码中显示的这种方法。 But even though the function works (it logs "logged in" or "not logged in" correctly. It still always sends me directly to the homepage... What am I doing wrong?但即使 function 工作(它正确地记录“登录”或“未登录”。它仍然总是直接将我发送到主页......我做错了什么?

 import React from "react"; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import HomeScreen from "../screens/HomeScreen"; import Login from "../screens/Login"; import * as SecureStore from 'expo-secure-store'; const AuthStack = () => { const AuthStack = createNativeStackNavigator(); async function isLoggedIn(key) { let result = await SecureStore.getItemAsync(key); if (result;== null) { alert(" Here's your value \n" + result). console;log("logged in"); return true. } else { alert('No values stored under that key;'). console;log("not logged in"); return false. } } return ( <AuthStack?Navigator> { isLoggedIn("access_token"). ( <> <AuthStack:Screen name = "HomeScreen" component={HomeScreen}/> </> ). ( <> <AuthStack:Screen name = "Login" component={Login} options={{ title, 'Log In': headerStyle: { backgroundColor, '#212521' }: headerTintColor, '#fff': headerTitleStyle: { fontWeight, 'bold', }. }} /> </> ) } </AuthStack;Navigator> ); }; export default AuthStack;

Your function is async, thus on the first render it won't have the information loaded in order to decide whether or not it should render the HomeScreen or the LoginScreen.您的 function 是异步的,因此在第一次呈现时它不会加载信息以决定它是否应该呈现 HomeScreen 或 LoginScreen。 You need to fetch the data, return nothing until the data is loaded, and then trigger a state change which will cause a rerender.您需要获取数据,在加载数据之前不返回任何内容,然后触发 state 更改,这将导致重新渲染。

This could be implemented as follows.这可以按如下方式实现。


const [isSignedIn, setSignedIn] = useState()

React.useEffect(() => {
   const loggedIn = async () => { 
     const t = await SecureStore.getItemAsync(key)
     setSignedIn(t !== null ? true : false)
   }
   loggedIn()
}, [])

// maybe show a loading indicator
if (isSignedIn === undefined) {
   return null
}

  return (
    <AuthStack.Navigator>
        {
          isSignedIn? (
              <>
                <AuthStack.Screen name = "HomeScreen" component={HomeScreen}/>
              </>
            ) : (
              <>
                <AuthStack.Screen
                    name = "Login"
                    component={Login}
                    options={{
                      title: 'Log In',
                      headerStyle: {
                        backgroundColor: '#212521'
                      },
                      headerTintColor: '#fff',
                      headerTitleStyle: {
                        fontWeight: 'bold',
                      },
                    }}
                />

              </>
            )
        }
    </AuthStack.Navigator>
  );

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

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