javascript - 如何检测应用程序在 react native 应用程序中的首次启动?

标签 javascript android ios typescript react-native

在我的场景中,我有三个不同的屏幕,例如 Page1Page2Page3。在这里,如果用户上次访问页面 2,那么下次如果用户打开应用程序,则不需要显示页面 1,而是显示页面 2。如何使用 react-native 应用程序实现此目的?

我尝试使用异步存储,但不知道如何管理多个页面

AsyncStorage.getItem("alreadyLaunched").then(value => {
            if(value == null){
                 AsyncStorage.setItem('alreadyLaunched', true); // No need to wait for `setItem` to finish, although you might want to handle errors
                 this.setState({firstLaunch: true});
            }
            else{
                 this.setState({firstLaunch: false});
            }}) // Add some error handling, also you can simply do this.setState({fistLaunch: value == null})

App.js

import { createAppContainer } from 'react-navigation';
import { createStackNavigator} from 'react-navigation-stack';

import FirstPage from './pages/FirstPage';
import SecondPage from './pages/SecondPage';
import ThirdPage from './pages/ThirdPage';

//import all the screens we are going to switch 
const App = createStackNavigator({
   //Constant which holds all the screens like index of any book 
   FirstPage: { screen: FirstPage, header: null},
   SecondPage: { screen: SecondPage,  headerLeft: null, headerBackTitle: null}, 
   ThirdPage: { screen: ThirdPage}, 
  },
  {
    initialRouteName: 'FirstPage',
  }
);
export default createAppContainer(App);

最佳答案

您应该使用 AsyncStorage 来存储当前屏幕,因此任何时候您从一个屏幕移动到另一个屏幕时都应该调用

AsyncStorage.setItem('screen', 'nameOfYourScreen')

在 App.js 中你应该调用

AsyncStorage.getItem('screen');

并将其影响到initialRouteName

PS:getItem() 是异步的。

关于javascript - 如何检测应用程序在 react native 应用程序中的首次启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61183597/

相关文章:

javascript - 事件监听器完成后运行函数

android - java.lang.RuntimeException : Unknown animation name: objectAnimator

android - 如何以编程方式固定 View 在线性布局中的位置

ios - 为什么applicationFrame和statusBarFrame是从两个不同的对象获取的?

ios - 重置图标角标(Badge)计数

javascript - jQuery 展开和折叠列表

Javascript for 循环新条件

javascript - 如何用空格替换下划线?

iphone - 将签名图像添加到 iOS 中的 pdf,尤其适用于 iPhone/iPad

android - 使用 Glide 4.11 时出现 "trying to use a recycled bitmap"崩溃,转换会导致此问题吗?