How to detect first launch in react-native

前端 未结 2 1126
臣服心动
臣服心动 2020-12-23 10:10

What is a good way to detect the first and initial launch of an react-native app, in order to show an on-boarding/introductory screen ?

相关标签:
2条回答
  • 2020-12-23 11:02

    I made some adjustments to martinarroyo's suggestion. AsyncStorage.setItem should set a string value and not a bool.

    import { AsyncStorage } from 'react-native';
        
    const HAS_LAUNCHED = 'hasLaunched';
    
    function setAppLaunched() {
      AsyncStorage.setItem(HAS_LAUNCHED, 'true');
    }
    
    export default async function checkIfFirstLaunch() {
      try {
        const hasLaunched = await AsyncStorage.getItem(HAS_LAUNCHED);
        if (hasLaunched === null) {
          setAppLaunched();
          return true;
        }
        return false;
      } catch (error) {
        return false;
      }
    }
    

    This function can then be imported wherever you need it. Note that you should render null (or something else clever) while waiting for the async function to check AsyncStorage.

    import React from 'react';
    import { Text } from 'react-native';
    import checkIfFirstLaunch from './utils/checkIfFirstLaunch';
    
    export default class App extends React.Component {
      constructor(props) {
        super(props);
    
        this.state = {
          isFirstLaunch: false,
          hasCheckedAsyncStorage: false,
        };
      }
    
      async componentWillMount() {
        const isFirstLaunch = await checkIfFirstLaunch();
        this.setState({ isFirstLaunch, hasCheckedAsyncStorage: true });
      }
    
      render() {
        const { hasCheckedAsyncStorage, isFirstLaunch } = this.state;
    
        if (!hasCheckedAsyncStorage) {
          return null;
        }
    
        return isFirstLaunch ?
          <Text>This is the first launch</Text> :
          <Text>Has launched before</Text>
        ;
      }
    }
    
    0 讨论(0)
  • 2020-12-23 11:10

    Your logic should follow this:

    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/>
           }
    }
    

    Hope it helps

    0 讨论(0)
提交回复
热议问题