【问题标题】:form submit re-renders page in react表单提交在反应中重新呈现页面
【发布时间】:2020-07-21 14:57:01
【问题描述】:

我在反应组件中有一个表单。

当我点击提交按钮时,我调用了signin()。在 signin() 中,出现错误,因为我可以在 chrome 控制台中看到输出,但它闪烁得如此之快,我无法阅读它。然后页面刷新,错误消息从控制台消失了。

为什么我的表单会重新呈现页面?以及如何抑制重新渲染以便读取错误?

这是我的组件:

import React, { Component } from 'react';
import './login.scss';
import axios from '../axios/axiosInstance';

class Login extends Component {

    constructor() {
        super();

        this.usernameRef = React.createRef();
        this.passwordRef = React.createRef();
    }

    signin() {
        axios.get('/auth', {headers: {
            username: this.usernameRef.current.value,
            password: this.passwordRef.current.value}}).then(response => {
                console.log('response=', response);
            }).catch(err => {
                console.log('err=', err);
            });
    }

    render() {

        return (
            <div className="login-container">
                <form onSubmit={this.signin}>
                    <div className="flex-row-end">
                        <div className="form-element flex-column-end">
                            <input type="text"
                                placeholder="Username or email"
                                name="username"
                                ref={this.usernameRef}
                                required />
                        </div>
                        <div className="form-element flex-column-end">
                            <input type="password"
                                placeholder="Password"
                                name="password"
                                ref={this.passwordRef}
                                required />
                        </div>
                        <div className="login-submit">
                            <button className="submit-login-button" type="submit"><i className="fas fa-sign-in-alt">&nbsp;&nbsp;&nbsp;</i>Sign In</button>
                        </div>
                    </div>
                </form>
            </div>
        );
    }
};

export default Login;

如您所见,在 signin() 中,我使用 axios 向我的后端发送带有用户凭据的请求。在后端,日志显示没有收到请求的记录。因此,错误必须在请求发送之前发生。但是我需要一种方法来抑制页面的重新呈现,以便我可以看到错误消息是什么。

非常感谢。

【问题讨论】:

    标签: reactjs forms submit rerender


    【解决方案1】:

    更改signin 的签名以获取表单提交事件,您需要阻止对其进行默认操作。这样可以防止页面重新加载。

    signin(e) {
      e.preventDefault();
      ...
    

    【讨论】:

      【解决方案2】:

      该错误告诉您尚未定义来自组件的引用。 因为方法在用作函数时没有绑定到this:

       <form onSubmit={e => this.signin(e)}>
      

      然后将e.preventDefault() 放在signin(e) 中,防止提交表单后闪烁。

      【讨论】:

        【解决方案3】:

        如果我没记错的话 axios 会使 http 请求异步,所以如果不是这种情况,您可能必须使用event.persist(),preventDefault() 应该像上面答案中提到的那样工作,也是recommended to call the api in thecomponentDidMount()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-02-28
          • 1970-01-01
          • 1970-01-01
          • 2013-11-19
          • 2019-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多