我正在使用与轴的反应天然。当我单击按钮中时,它应该重定向到HomeScreen,但不起作用。当我用PrivateCreen()替换它时;使用警报("成功登录。");它在MSG中成功登录,但使用PrivateCreen();它给出了"无效的用户名或密码"味精。这是我在login.js中使用Handlelogin的代码:
import React, { Component } from 'react';
import axios from 'axios';
import PrivateScreen from './getonAuthscreen';
handleLogin = () => {
const{ username, password } = this.state;
if(username && password){
axios
.post('http://192.168.X.X:3000/user/login', {
username,
password,
})
.then(response =>{
PrivateScreen();
}).catch (()=>{
alert('Invalid Username or password.');
});
// return alert('you may pass');
}else{
alert('Please check username and password correctly.'); } };
这是homescreen.js的代码:
import React from 'react';
import {
View,Text
} from 'react-native';
class HomeScreen extends React.Component {
static navigationOptions = {
title: 'Welcome to the app!',
};
render() {
return (
<View>
<Text>TESTSTST</Text>
</View>
);
}
}
export default HomeScreen;
和getonauthscreen.js
的代码 import { createSwitchNavigator, createStackNavigator, createAppContainer } from 'react-navigation';
import HomeScreen from './HomeScreen';
// Implementation of HomeScreen, OtherScreen, SignInScreen, AuthLoadingScreen
// goes here.
const startSingleScreenApp = createStackNavigator(
{ HomeScreen: HomeScreen,
}
);
//const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createAppContainer(createSwitchNavigator(
{
App: startSingleScreenApp,
},
));
尝试遵循反应范围的验证流。示例
export default createAppContainer(createSwitchNavigator(
{
App: AppStackNavigator,
Auth: AuthStack,
},
{
initialRouteName: 'Auth',
},
));
this.props.navigation.navigate(userToken ? 'App' : 'Auth');