javascript - 如何在反应原生应用程序中检测应用程序首次启动?
问题描述
在我的场景中,我有三个不同的屏幕,如Page1
, Page2
, Page3
. 在这里,如果用户上次访问页面 2,那么下次如果用户打开应用程序,而不是显示 page1 需要显示 page2。如何使用react-native
应用程序实现这一点?
我尝试使用async
存储但不知道如何管理多个页面
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})
应用程序.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() 是异步的。
推荐阅读
- node.js - Sequelize 4.42.0:设置实例方法返回“不是函数”错误
- python - 由于 for 循环,打印功能失败
- ios - 使用动态单元格的高度重新加载后保持 UICollectionView 位置
- ios - Xcode 10 Alamofire 命令 PhaseScriptExecution 失败,退出代码非零
- android - 如何防止 LTR 布局在某些 Android 设备上被强制 RTL?
- react-native - 如果上一行有足够的空间,则分两行显示文本组件
- jhipster - 如何最小化 jhipster 微服务环境的内存占用?
- firebase - 从firebase中的数组中删除特定的地图/对象
- c# - 两个表 LinqToSql 的链接
- ubuntu - 从挂起 Ubuntu Budgie 18.10 唤醒后黑屏