简体   繁体   English

在 React 中提交表单后呈现错误消息

[英]Rendering an error message after form submission in React

I'm still new to React, so got stuck on this and can't get my head around it.我还是 React 的新手,所以被困在这个问题上,无法理解它。 I am building a login form and want to display an error message if the autentication fails.我正在构建一个登录表单,如果验证失败,我想显示一条错误消息。 I have created a state variable which would contain the error message and use handleSubmit function to call the "login" function.我创建了一个 state 变量,它将包含错误消息并使用 handleSubmit function 来调用“登录”function。

Here is my Login component:这是我的登录组件:

import React from 'react'
import {auth} from './Functions.js';


class Login extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      email: '',
      pass: '',
      error: null
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange (e) {
    const { name, value } = e.target;
    this.setState({ [name]: value });
  }

  handleSubmit(e) {
    e.preventDefault();
    var response = auth.login(this.state);
    if(!response){
      this.setState({error : auth.error});
    }
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        {this.state.error != null && <p>{this.state.error}</p> }
        <input type="text" name="email" onChange={this.handleChange}/>
        <input type="text" name="pass" onChange={this.handleChange}/>
        <button type="submit">Login</button>
      </form>
    );
  }
}

export default Login;

The login function is in the Functions.js file (which works in returning the error message):登录 function 位于 Functions.js 文件中(用于返回错误消息):

export const auth = {
    login,
    error: null,
  }

function login( data ){
  if(data.email === 'test@test.com'){
    return true;
  }else{
    auth.error = 'error message';
    return false;
  }
}

However - as I am using Firebase authorization, the login function looks like this, so when I get an error back - it does not get displayed to the user (not until I click on the login button more than once):但是 - 由于我使用的是 Firebase 授权,登录 function 看起来像这样,所以当我收到错误时 - 它不会显示给用户(直到我多次单击登录按钮):

function login( data ){
    Firebase.auth().signInWithEmailAndPassword(data.email, data.password)
    .then(function(result) {    
      return true;
    }).catch(function(error) {
      auth.error = error.message;
      return false;
    });
}

Any ideas why this might be happening?任何想法为什么会发生这种情况?

you need to handle the auth.login function asynchronously with.then and.catch.您需要使用.then 和.catch 异步处理 auth.login function。 Asynchronous code is contagious - When one function is asynchronous the whole chain of functions call becomes asynchronous and needs to be handle accordingly.异步代码具有传染性 - 当一个 function 是异步的时,整个函数调用链变得异步,需要相应地处理。

The login function is an asynchronous function when you changed it to use Firebase authentication.当您将其更改为使用Firebase身份验证时,登录 function 是asynchronous function。 So when doing this:所以当这样做时:

handleSubmit(e) {
    e.preventDefault();
    var response = auth.login(this.state); // this will be an asynchronous function which means the response will not come instantly.
  }

Change the function to将 function 更改为

handleSubmit = async (e) => {
var response = await auth.login(this.state); // check the form of response that would return from firebase and accordingly catch the error.
if(!response){
  this.setState({error : auth.error});
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM