React Native的登录身份验证问题

时间:2020-06-21 16:07:41

标签: javascript react-native

我在登录身份验证页面上遇到问题。该应用程序不会让我输入任何用户名。相反,它会生成提示“输入用户名和密码”的提示:prompt

***LoginScreen.js***

import React from 'react'
import{View,Text,StyleSheet,TextInput,TouchableOpacity}from 'react-native'
import axios from 'axios'
import AsyncStorage from '@react-native-community/async-storage'

class LoginScreen extends React.Component{

    state={
      username:"",
      password:"",
      loading:false
    }
     onChangeHandle(state,value){
       this.setState({
           [state]:value
       })
     }
     dologin(){
     const{username,password}=this.state
     if(username && password){
        const req={
            "email":username,
            "password":password
        }
        this.setState({
            loading:true
        })
        axios.post("https://reqres.in/api/login",req)
        .then(
            res=>{
               this.setState({
                   loading:false,
               })
               AsyncStorage.setItem("token",res.data.token)
               .then(
                   res=>{
                    this.props.navigation.navigate("App")
                    alert("Login Successful")
                   }
               )
               
            },
            err=>{
                this.setState({
                    loading:false
                })
                alert("User Name Password is wrong")
            }
        )
        }
     
     else{
         alert("Enter Username and password")
     }
    }
     
    render(){
        const{username,password,loading}=this.state
        return(
            <View
            style={styles.container}
            >
                <View
                style={styles.formWrapper}
                >
                    <Text
                    style={styles.welcomeText}
                    >Welcome Back User            
                    </Text>
                    <View
                    style={styles.formRow}
                    >
                    <TextInput
                    style={styles.textInput}
                       placeholder="Enter username"
                       placeholderTextColor="#333"
                       value={username}
                       onChangeText={(value)=>this.dologin()}
                       disabled={loading}
                      />
                      </View>
                      <View
                      style={styles.formRow}
                      >
                      <TextInput
                      style={styles.textInput}
                       placeholder="Enter password"
                       placeholderTextColor="#333"
                       secureTextEntry={true}
                       value={password}
                       onChangeText={(value)=>this.dologin('password',value)}
                      />
                    </View>
                    <TouchableOpacity
                    activeOpacity={0.8}
                    style={{
                        ...styles.signinBtn,
                        backgroundColor:loading ? "#ddd" :"blue"
                    }}
                    onPress={()=>this.dologin()}
                    disabled={loading}
                    >
                    <Text
                    style={styles.signinText}
                        >
                            {loading ? "Loading...":"SignIn"}
                        </Text>
                    </TouchableOpacity>
                </View>
            </View>
        )
    }
}
export default LoginScreen;

const styles=StyleSheet.create({
    container:{
        height:'100%',
        alignItems:'center',
        justifyContent:"center"
    },
    formWrapper:{
        width:'80%',
        marginBottom:10
    },
    formRow:{ 
    marginBottom:10
    },
    textInput:{
     backgroundColor:'#ddd',
     height:40,
     paddingHorizontal:10,
     color:'#333'
    },
   welcomeText:{
       textAlign:'center',
       marginBottom:30,
       fontSize:24,
       fontWeight:"bold"
   },
   signinBtn:{
    paddingVertical:10
   },
   signinText:{
   textAlign:"center",
   color:'#fff',
   fontSize:18,
   fontWeight:"bold"

   }
})

我似乎找不到该错误的根本原因。我已经阅读了很多次调试代码。现在已经有几个小时了,实际上非常令人沮丧。

1 个答案:

答案 0 :(得分:2)

您正在调用方法登录,只要输入值的值发生更改,您只需在单击按钮时只调用一次此方法,并且要使用输入框的值,则需要使用this.setState方法附加和可以帮助您的示例代码。

  <TextInput
                    style={styles.textInput}
                       placeholder="Enter username"
                       placeholderTextColor="#333"
                       value={username}
                       **`onChangeText={value=>this.setState({username:value })}`**
                       disabled={loading}
                      />
                      </View>
                      <View
                      style={styles.formRow}
                      >
                      <TextInput
                      style={styles.textInput}
                       placeholder="Enter password"
                       placeholderTextColor="#333"
                       secureTextEntry={true}
                       value={password}
                       **onChangeText={value=>this.setState({password:value})}**
                      />
                    </View>
                    <TouchableOpacity
                    activeOpacity={0.8}
                    style={{
                        ...styles.signinBtn,
                        backgroundColor:loading ? "#ddd" :"blue"
                    }}
                    onPress={()=>this.dologin()}
                    disabled={loading}
                    >