【发布时间】:2026-02-06 14:20:04
【问题描述】:
问题很简单,我知道同一个问题有很多答案,但我设置了自己的 App.js 文件,我几乎尝试了所有但没有成功。
所以我的App.js 看起来像这样
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
StatusBar
} from 'react-native';
import { createStackNavigator } from 'react-navigation';
import { Header, Button, Spinner } from './src/components/comman';
import SplashScreen from 'react-native-splash-screen';
import LoginForm from './src/components/LoginForm';
import SignupForm from './src/components/SignupForm';
import Home from './src/components/Home';
import ForgetPassword from './src/components/ForgetPassword';
import { YellowBox } from 'react-native';
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);
const App = createStackNavigator ({
Login: { screen: LoginForm },
Home: { screen: Home },
Signup: { screen: SignupForm },
ForgetPassword: { screen : ForgetPassword },
},
{
initialRouteName: 'Login',
});
export default App;
现在我将登录状态存储在AsyncStorage 我的LoginForm.js 中,就像这样
.
.
AsyncStorage.setItem('isLoggedIn', 'true').then((a) => {
this.onLoginSuccess(responseData.user_id);
});
.
.
所以现在我想根据我的本地存储元素设置initialRouteName。
简单如果isLoggedIn 是true 然后initialRouteName : Home 否则initialRouteName : Login
简单但不知道如何在my App.js 中使用它
请帮忙:)
【问题讨论】:
-
而不是这样做 if 和 else 使用重定向。即使我遇到了很多问题,最后还是使用了重定向方法。
标签: react-native react-navigation