无法导航到另一个屏幕进行本地反应

时间:2019-02-26 07:28:20

标签: react-native react-navigation

我正在使用带有axios的本机反应。当我单击“登录”按钮时,它应重定向到“主屏幕”,但它不起作用。当我用Privatescreen()替换它时;带有警报(“已成功登录。”);它可以成功登录msg,但带有privatescreen();其给出“无效的用户名或密码”消息。这是我的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,
  },

));

1 个答案:

答案 0 :(得分:0)

尝试从react-navigation遵循Auth流。 Example


export default createAppContainer(createSwitchNavigator(
  {
    App: AppStackNavigator,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'Auth',
  },
));

this.props.navigation.navigate(userToken ? 'App' : 'Auth');