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

前端之家收集整理的这篇文章主要介绍了如何检测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/>
       }
}

希望能帮助到你

原文链接:https://www.f2er.com/react/301284.html

猜你在找的React相关文章