加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:54:22 所属栏目:百科 来源:网络整理
导读:有什么方法可以检测反应原生应用程序的首次启动和初始启动,以显示入门/介绍屏幕? 你的逻辑应该遵循这个: class MyStartingComponent extends React.Component { constructor(){ super(); this.state = {firstLaunch: null}; } componentDidMount(){ AsyncS
有什么方法可以检测反应原生应用程序的首次启动和初始启动,以显示入门/介绍屏幕?
你的逻辑应该遵循这个:
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/>
       }
}

希望能帮助到你

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读