【发布时间】:2016-08-06 08:01:35
【问题描述】:
我正在使用 react-native-router-flux 构建一个 React Native 项目,但导航有点问题。基本上,我正在构建一个登录页面,该页面使用数据库检查电子邮件/密码,并在匹配时存储访问令牌。然后我想立即从登录页面导航到主页,但我不知道如何实现。似乎 {Actions.SceneKey} 仅在 onPress 时有效。这是我的登录页面代码:
'use strict'
import React, { Component } from 'react';
import { AsyncStorage, StyleSheet, Text, View, TouchableHighlight, AlertIOS,} from 'react-native';
import { Actions } from 'react-native-router-flux'
import t from 'tcomb-form-native'
import ViewContainer from '../components/ViewContainer';
import StatusBarBackground from '../components/StatusBarBackground';
var STORAGE_KEY = 'access_token';
var Form = t.form.Form;
var Person = t.struct({
email: t.String,
password: t.String
});
const options = {};
var LoginIndexScreen = React.createClass({
async _onValueChange(item, selectedValue) {
try {
await AsyncStorage.setItem(item, selectedValue);
{Actions.HomeScreen}
} catch (error) {
console.log('AsyncStorage error: ' + error.message);
}
},
_userSignup() {
var value = this.refs.form.getValue();
if (value) { // if validation fails, value will be null
fetch("https://EXAMPLE.herokuapp.com/oauth/token", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: value.email,
password: value.password,
})
})
.then(console.log(response))
.then((response) => response.json())
.then((responseData) => {
this._onValueChange(STORAGE_KEY, responseData.id_token),
AlertIOS.alert(
"Signup Success!"
)
})
.done();
}
},
_userLogin() {
var value = this.refs.form.getValue();
if (value) { // if validation fails, value will be null
fetch("https://EXAMPLE.herokuapp.com/oauth/token", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: value.email,
password: value.password,
})
})
.then((response) => response.json())
.then((responseData) => {
if (responseData.access_token) {
console.log(responseData)
this._onValueChange(STORAGE_KEY, responseData.access_token)
{Actions.HomeScreen}
} else {
AlertIOS.alert(
"Login failed due to: " + responseData.message
)
}
})
.done();
}
},
render() {
return (
<View style={styles.container}>
<View style={styles.row}>
<Form
ref="form"
type={Person}
options={options}
/>
</View>
<View style={styles.row}>
<TouchableHighlight style={styles.button} onPress={this._userSignup} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Signup</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.button} onPress={this._userLogin} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>Login</Text>
</TouchableHighlight>
</View>
</View>
);
}
});
var styles = StyleSheet.create({
container: {
justifyContent: 'center',
marginTop: 50,
padding: 20,
backgroundColor: '#ffffff',
},
title: {
fontSize: 30,
alignSelf: 'center',
marginBottom: 30
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
button: {
height: 36,
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
},
});
module.exports = LoginIndexScreen;
任何帮助将不胜感激
【问题讨论】:
标签: javascript navigation react-native