如何基于AsyncStorage值在React Native应用中打开初始屏幕?

时间:2019-02-13 07:18:16

标签: react-native asyncstorage

我是React-native的新手。我正在使用1.用户注册模块AppNavigator和2. 2.应用程序TabNaviagtor

的内容部分创建一个应用程序

当用户首次登录时,我将用户凭据存储在react-native中的AsyncStorage中。从下一次开始,当凭据可用时,应该打开内容页面(TabNavigator),否则打开AppNavigator页面。

请帮助,我该如何实现?

App.Js

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,ImageBackground} from 'react-native';
import AppNavigator from './Components/AppNavigator/AppNavigator'
import bgImage  from './Components/Assets/Login_BG_Image.png'
import TabNavigator from './Components/TabNavigator/TabNavigator';
import {AsyncStorage} from 'react-native';

export default class App extends Component {

  state = {
    isLogin : false
  }

  loginHandler = async() =>
  {
    console.log('isLogin')
    const isLogin = await AsyncStorage.getItem('emailID')
    console.log(isLogin + 'value')
    if (isLogin !== undefined) {
      console.log('Is Login Already Done show  TabBar')
      return (
        <TabNavigator />
      );
    }else 
    {
      console.log('Show Login Page')
      return (
        <AppNavigator />
      );
    }
  }

  render() {
    return (
      this.loginHandler()
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex : 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
  bgImage_container : {
    flex : 1,
    width : '100%',
    height : '100%'    
  }
});

This is the error I am getting

如果我更改代码

loginHandler = () =>
  {
    console.log('isLogin')
    const isLogin =  AsyncStorage.getItem('emailID')
    console.log(isLogin + 'value')
    if (isLogin !== undefined) {
      console.log('Is Login Already Done show  TabBar')
      return (
        <TabNavigator />
      );
    }else 
    {
      console.log('Show Login Page')
      return (
        <AppNavigator />
      );
    }
  }

isLogin获得承诺对象

isLogin.value显示为未定义。

0 个答案:

没有答案