简体   繁体   English

使用 React & Bootstrap 有条件地渲染空 div 或错误

[英]Conditionally render empty div or error with React & Bootstrap

I'm setting up a signup form that displays errors below the input fields if the user makes a mistake.我正在设置一个注册表单,如果用户出错,它会在输入字段下方显示错误。 The way I have it setup right now, the form will add a div with the error below when the user tries to submit their info.按照我现在设置的方式,当用户尝试提交他们的信息时,表单将添加一个带有以下错误的 div。 My issue is that when there's an error, it adds the div and messes up the layout of the form because it has to move everything to make space for each error.我的问题是,当出现错误时,它会添加 div 并弄乱表单的布局,因为它必须移动所有内容以为每个错误腾出空间。 Is there a way to just have an empty div there if there isn't any errors so that it doesn't mess with the layout when there is one?如果没有任何错误,有没有办法在那里只有一个空的 div,这样它就不会在有一个时弄乱布局? So like, instead of having margin for spacing between fields, it's an empty div for the errors.就像,而不是字段之间的间距有余量,它是错误的空 div。

import React, { Component } from "react";
import axios from "axios";
import classnames from "classnames";

import "./Signup.css";

class Signup extends Component {
  constructor() {
    super();
    this.state = {
      username: "",
      email: "",
      password: "",
      errors: {}
    };

    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }

  onChange(e) {
    this.setState({ [e.target.name]: e.target.value });
  }
  onSubmit(e) {
    e.preventDefault();

    const newUser = {
      username: this.state.username,
      email: this.state.email,
      password: this.state.password
    };

    axios
      .post("api/users/register", newUser)
      .then(res => console.log(res.data))
      .catch(err => this.setState({ errors: err.response.data }));
  }

  render() {
    const { errors } = this.state;

    return (
      <div className="signup-form">
        <form noValidate onSubmit={this.onSubmit}>
          <h2>Sign Up</h2>
          <p>It's free and only takes a minute.</p>
          <hr />
          <div className="form-group">
            <label>Username</label>
            <input
              type="text"
              name="username"
              className={classnames("form-control form-control-md", {
                "is-invalid": errors.username
              })}
              value={this.state.username}
              onChange={this.onChange}
            />
            {errors.username && (
              <div className="invalid-feedback">{errors.username}</div>
            )}
          </div>
          <div className="form-group">
            <label>Email</label>
            <input
              type="text"
              name="email"
              className={classnames("form-control form-control-md", {
                "is-invalid": errors.email
              })}
              value={this.state.email}
              onChange={this.onChange}
            />
            {errors.email && (
              <div className="invalid-feedback">{errors.email}</div>
            )}
          </div>
          <div className="form-group">
            <label>Password</label>
            <input
              type="text"
              name="password"
              className={classnames("form-control form-control-md", {
                "is-invalid": errors.password
              })}
              value={this.state.password}
              onChange={this.onChange}
            />
            {errors.password && (
              <div className="invalid-feedback">{errors.password}</div>
            )}
          </div>
          <div className="form-group">
            <button type="submit" className="btn btn-primary btn-block btn-lg">
              Sign Up
            </button>
          </div>
          <p className="small text-center">
            By clicking the Sign Up button, you agree to our <br />
            <a href="#">Terms &amp; Conditions</a>, and{" "}
            <a href="#">Privacy Policy</a>
          </p>
          <div className="text-center">
            Already have an account? <a href="#">Login here</a>
          </div>
        </form>
      </div>
    );
  }
}

export default Signup;

Yes, you can use visibility:hidden property of css.是的,您可以使用 CSS 的visibility:hidden属性。

 <div style={{ visibility: error.email? 'visible': 'hidden'}}></div>

since visibility always takes up space, in both cases it is visible as well as hidden.由于可见性总是占用空间,在这两种情况下,它既可见又隐藏。 so it won't mess with the layout.所以它不会弄乱布局。

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

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