Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 在StackNavigator中处理react native中登录的用户_React Native - Fatal编程技术网

React native 在StackNavigator中处理react native中登录的用户

React native 在StackNavigator中处理react native中登录的用户,react-native,React Native,我有如下代码: function RootNavigator() { const [isLoggedIn, setIsLoggedIn] = useState(false) isUserLoggedIn().then((someBoolean) => { setIsLoggedIn(someBoolean) }) console.log("LOGEED IN STSTE:", isLoggedIn) return ( &

我有如下代码:

function RootNavigator() {
  const [isLoggedIn, setIsLoggedIn] = useState(false)

  isUserLoggedIn().then((someBoolean) => {
    setIsLoggedIn(someBoolean)
  })
  
    console.log("LOGEED IN STSTE:", isLoggedIn)
   return (
    <Stack.Navigator screenOptions={{ headerShown: false }}>
      {isLoggedIn || <Stack.Screen name="Root" component={WelcomeScreen} />}
      {isLoggedIn && <Stack.Screen name="InvestorProfileQuiz" component={InvestorProfileQuizScreen} />}
      {isLoggedIn || <Stack.Screen name="AppTour" component={AppTourScreen} />}
      {isLoggedIn || <Stack.Screen name="Login" component={LoginScreen} />}
      {isLoggedIn || <Stack.Screen name="NotFound" component={NotFoundScreen} options={{ title: 'Oops!' }} />}
    </Stack.Navigator>
  );
}
函数RootNavigator(){
const[isLoggedIn,setIsLoggedIn]=useState(false)
isUserLoggedIn()。然后((someBoolean)=>{
setIsLoggedIn(someBoolean)
})
log(“STSTE中的LOGEED:”,isLoggedIn)
返回(
{伊斯洛格丁| |}
{isLoggedIn&&}
{伊斯洛格丁| |}
{伊斯洛格丁| |}
{伊斯洛格丁| |}
);
}

所以问题是在渲染时,
isLoggedIn
段状态为false,然后第一页
WelcomeScreen
渲染。屏幕闪烁,然后
isLoggedIn
更改为true,然后
InvestorProfileQuike
正确呈现。如何确保async/await方法
isUserLoggedIn()
在呈现StackNavigtor子级之前解析?

这是我通常处理登录和注销状态的方式

function RootNavigator() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);

  // Passing an empty array as second argument
  // makes useEffect behave like componentDidMount
  useEffect(() => {
    
    // Wrap logic in the async init function
    // since the useEffect callback can't be async
    async function init() {
      const someBoolean = await isUserLoggedIn();
      setIsLoggedIn(someBoolean);
    }
    init();
  }, []);

  // Return null or your logged out screen component here.
  if (!isLoggedIn) {
    return null;
  }

  return (
    <Stack.Navigator screenOptions={{headerShown: false}}>
      <Stack.Screen name="Root" component={WelcomeScreen} />}
      <Stack.Screen
        name="InvestorProfileQuiz"
        component={InvestorProfileQuizScreen}
      />
      <Stack.Screen name="AppTour" component={AppTourScreen} />
      <Stack.Screen name="Login" component={LoginScreen} />(
      <Stack.Screen
        name="NotFound"
        component={NotFoundScreen}
        options={{title: 'Oops!'}}
      />
    </Stack.Navigator>
  );
}
函数RootNavigator(){
const[isLoggedIn,setIsLoggedIn]=useState(false);
//将空数组作为第二个参数传递
//使useEffect的行为类似于componentDidMount
useffect(()=>{
//异步初始化函数中的包装逻辑
//因为useEffect回调不能是异步的
异步函数init(){
const someBoolean=await isUserLoggedIn();
setIsLoggedIn(someBoolean);
}
init();
}, []);
//在此处返回null或您的注销屏幕组件。
如果(!isLoggedIn){
返回null;
}
返回(
}
(
);
}