gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:07:39 24 4
gpt4 key购买 nike

在我的场景中,我有三个不同的屏幕,例如 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com