gpt4 book ai didi

react-native - 如何检测react-native中的首次启动

转载 作者:行者123 更新时间:2023-12-03 13:12:43 25 4
gpt4 key购买 nike

检测 react-native 应用程序的首次和初始启动以显示登录/介绍屏幕的好方法是什么?

最佳答案

你的逻辑应该遵循这个:

class MyStartingComponent extends React.Component {
constructor(){
super();
this.state = {firstLaunch: null};
}
componentDidMount(){
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})
}
render(){
if(this.state.firstLaunch === null){
return null; // This is the 'tricky' part: The query to AsyncStorage is not finished, but we have to present something to the user. Null will just render nothing, so you can also put a placeholder of some sort, but effectively the interval between the first mount and AsyncStorage retrieving your data won't be noticeable to the user.
}else if(this.state.firstLaunch == true){
return <FirstLaunchComponent/>
}else{
return <NotFirstLaunchComponent/>
}
}

希望能帮助到你

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

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