React-native IOS和Android中的自动登录问题
问题描述:
我正在使用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);
答
我don'nt看到你的代码,你在哪里存储值在localStorage的 所以按照我的看法,你可以用这种方式来存储和获取价值形态的localStorage
AsyncStorage.setItem('user', JSON.stringify(email));
并获取数据,你应该使用解析方法
AsyncStorage.getItem('user', (err, result) => {
const usermail = JSON.parse(result);
console.log(useremail)
});
通过这种类型的,你不会得到你的价值 可能是这可以帮助你
请修改您的问题。尽可能纠正拼写和语法错误。不要以大写字母开头。 –
只是为了更好地理解你的问题,你想显示登录屏幕,如果用户为空? – gbland777
但值显示为空 – Lavaraju