2017-07-29 135 views
-1

我正在使用React Native开发示例应用程序。我的问题是,我获取的登录凭据保存在登录页面localstorage()异步存储中,该值存储在index.ios.js文件中,但值显示为空。React-native IOS和Android中的自动登录问题

中的任何一个,请告诉我,当我登录到go_to_main页面,显示login屏幕和主屏幕后,这是我的主要问题

显示我可以得到index.ios.js文件中的值之后。这里是我的代码:

/** 
* Sample React Native App 
* https://github.com/facebook/react-native 
* @flow 
*/ 

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    AsyncStorage, 
    View 
} from 'react-native'; 
import NavigationExperimental from 'react-native-deprecated-custom-components'; 
var loginScene //= require('./src/bundles/Login/scenes/LoginScene/index.js'); 
var SplashScreen = require('@remobile/react-native-splashscreen'); 

class SampleApp extends Component { 

    constructor(props) { 
     super(props); 
     this.state = { 
      email:'', 
      userName:'' 

     } 
    } 

    componentWillMount() { 


     AsyncStorage.getItem('Email',(err,Email)=>{ 
      if(Email == null){ 

      } 
      else{ 
      AsyncStorage.getItem('userName',(err,userName)=>{ 
       if(userName == null){ 

       } 
       else{ 
        this.setState({ 
        email:Email, 
        userName:userName 
        }) 
       } 

      }).done() 
      } 
      }).done() 
    } 

    render() { 
    if(this.state.email != null){ 

     loginScene = require('./src/bundles/Dashboard/scenes/koopiMainScene/index.js'); 
    } 
    else{ 
     alert("email1111:"+this.state.email) 
     loginScene = require('./src/bundles/Login/scenes/LoginScene/index.js'); 

    } 
    return (
     <View style={{flex:1,backgroundColor:'white'}}> 
      <NavigationExperimental.Navigator 
       initialRoute={{ 
       component:loginScene, 
       passProps:{menuTitle:this.state.userName, Email:this.state.email} 
       }} 
       configureScene={(route) => ({ 
        ...NavigationExperimental.Navigator.SceneConfigs.HorizontalSwipeJump, 
        gestures: false 
        })} 
       renderScene={(route, navigator) =>{ 
        return <route.component navigator={navigator} {...route.passProps} />; 
       }}/> 

     </View> 
    ); 
    } 
} 

AppRegistry.registerComponent('SampleApp',() =>SampleApp); 
+1

请修改您的问题。尽可能纠正拼写和语法错误。不要以大写字母开头。 –

+0

只是为了更好地理解你的问题,你想显示登录屏幕,如果用户为空? – gbland777

+0

但值显示为空 – Lavaraju

回答

0

我don'nt看到你的代码,你在哪里存储值在localStorage的 所以按照我的看法,你可以用这种方式来存储和获取价值形态的localStorage

AsyncStorage.setItem('user', JSON.stringify(email)); 

并获取数据,你应该使用解析方法

AsyncStorage.getItem('user', (err, result) => { 
    const usermail = JSON.parse(result); 
    console.log(useremail) 
}); 

通过这种类型的,你不会得到你的价值 可能是这可以帮助你

+0

感谢您的回应,实际上当我在此屏幕登录设置AsyncStorage.setItem('用户',JSON.stringify(电子邮件)); alerady我设置了,之后,我可以在起始页中使用这个我正在获取componentWillmount()方法。 – Lavaraju

+0

哪些值显示为空,用户名或电子邮件,让我知道我可以帮助这 –

+0

电子邮件只有请检查一次我的以上代码 – Lavaraju