什么是检测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相关问答推荐

对iOS通知上未显示的本机映像做出react

@react-native-community/cli-platform-ios/native_modules' 来自哪里?

如何渲染一个 svg 以在不同的窗口大小下动态地看起来相同?

react-redux connect 和 redux 数据的组件生命周期顺序

调用 ES6 方法时绑定上下文.如何从称为回调的方法中访问对象?

在 create-react-native-app 元素中突然看到错误Plugin/Preset files are not allowed to export objects, only functions

react native条件渲染

React Native 入门元素Bundle 失败,出现 Unexpected Token 错误

React Native:fetch request failed with error - TypeError: Network request failed(…)

React native - 创建单例模式的最佳方式

在 Android React Native 应用程序中正确使用 Intl

Console.error:no permission handler detected

react-native fetch 和基本身份验证

如何在 React Native 上获取设备的 ip?

在 React Native App 中禁用屏幕捕获/ScreenShot

如何知道 FlatList 中的滚动状态?

expo 已过期卸载并再次运行以升级

WebStorm + ReactNative?

react-native图像预取

NavigationActions.reset 不是函数吗?