Preface
Seeing that many companies are beginning to try to use ReactNative to achieve cross-platform development, I have also written many articles recently, hoping to help more students who want to know more get started quickly. ReactNative.
ReactNative's App guide page implementation logic
- Implementing the guide page in RN is much more complicated than the native implementation.
- Reasons:
- 1. The native configuration information info.plist file cannot be read in RN, so it is impossible to determine whether the current version is the latest version. If it is the latest version, the boot page will be displayed
- 2. The local storage of RN is asynchronous, not synchronous. This will cause an error to be reported at the beginning if you want to obtain the local storage information and judge whether to display the boot page or the homepage based on the storage information
- The reason for the error is very simple. As soon as the program starts, the interface needs to be displayed immediately, but due to asynchronousness, it cannot return so quickly.
Solution to the RN guide page:
- Write a startup interface by yourself, display the startup interface at the beginning
- Then after the startup interface is displayed, determine whether to display the boot page or the homepage later
How to judge whether to display the boot page or the homepage
- Enter the interface for the first time, write an attribute, and record the first load.
- Every time it is started, get whether the attributes loaded for the first time have been saved before. If it has been loaded, the home page will be displayed. If it has not been loaded, the boot page will be displayed.
App boot page Implementation code
/** * Created by ithinkeryz on 2017/5/15. */ import React, { Component } from 'react';import { AppRegistry, StyleSheet, Text, View, AsyncStorage, Image} from 'react-native';import Main from './Main/Main'import {Navigator} from 'react-native-deprecated-custom-components'import Guide from './Guide/Guide'import Common from './Common/Common'class LaunchView extends Component { render(){ return ( <Image source={{uri:'LaunchImage'}} style={{width:Common.screenW,height:Common.screenH}}/> ) } componentDidMount() { // 延迟点 setTimeout(this.openApp.bind(this),2000); // this.openApp(); } openApp(){ AsyncStorage.getItem('isFirst',(error,result)=>{ if (result == 'false') { console.log('不是第一次打开'); this.props.navigator.replace({ component:Main }) } else { console.log('第一次打开'); // 存储 AsyncStorage.setItem('isFirst','false',(error)=>{ if (error) { alert(error); } }); this.props.navigator.replace({ component:Guide }) } }); }}export default class App extends Component { // 渲染场景 _renderScene(route, navigator){ return ( <route.component navigator={navigator} {...route} /> ) } render() { // 判断是不是第一次打开 return ( <Navigator initialRoute={{ component: LaunchView }} renderScene={this._renderScene.bind(this)} style={{flex:1}} /> ); } }
Achievement effect
First time entering
react video tutorial"