如何检查以确保在React中单击按钮时填写表格?

问题描述

我有一个按钮,可以根据用户提供的数字进行计算并将结果填充到页面中。相同的按钮还将产品保存到数据库中,同时还用数据库中的那些相同产品直观地填充页面。因此,基本上,它显示了用户在字段中输入的任何内容的总数和先前的总数。我的问题是我想包括错误处理,以便如果数字不是0或NaN(因为它们没有填写所有表格),它将发出警报,也不会将数据推送到数据库。这是我的尝试,并且不起作用,如果结果为NaN,它仍会显示该结果并将NaN值推入数据库,我该如何实现?尝试如下:

handleClick = (event,billInfo) => {
event.preventDefault();
const dbRef = firebase.database().ref();

const result =
  (billInfo.amount / billInfo.group) * (billInfo.tip / 100 + 1);

// ATTEMPT START ======

result === NaN
  ? alert("Please fill all forms!")
  : dbRef.push({
      result: result.toFixed(2),name: billInfo.name,});

// ATTEMPT END ======

this.setState({
  total: result.toFixed(2),});

这是带有链接到主app.js的按钮的表单组件:

class inputForm extends Component {
  constructor() {
    super();
    this.state = {
      name: "",group: "",amount: "",tip: "",};
  }

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

  clearForm = (event) => {
    event.preventDefault();

    this.setState({
      name: "",});
  };

  render() {
    return (
      <form className="tipApp">
        <div>
          <label htmlFor="groupName">
            <i class="fas fa-id-card"></i>Group Name?
          </label>
          <input
            onChange={this.handleChange}
            type="text"
            id="groupName"
            value={this.state.name}
            placeholder="Name your group"
            name="name"
          />
        </div>

        <div>
          <label htmlFor="groupSize">
            <i class="fas fa-users"></i>How many?
          </label>
          <input
            onChange={this.handleChange}
            type="number"
            id="groupSize"
            value={this.state.group}
            min="1"
            step="1"
            placeholder="Add the group size"
            name="group"
            required
          />
        </div>

        <div>
          <label htmlFor="billAmount">
            <i class="fas fa-receipt"></i>How much?
          </label>
          <input
            onChange={this.handleChange}
            type="number"
            id="billAmount"
            value={this.state.amount}
            min="0"
            step=".01"
            placeholder="Add the bill total"
            name="amount"
            required
          />
        </div>

        <div>
          <label htmlFor="tipAmount">
            <i class="fas fa-heart"></i>Tip?
          </label>
          <input
            onChange={this.handleChange}
            type="number"
            id="tipAmount"
            value={this.state.tip}
            min="10"
            step="5"
            placeholder="Add the tip %"
            name="tip"
          />
        </div>

        <button onClick={(event) => this.props.getTotal(event,this.state)}>
          Split it!
        </button>
        <button onClick={this.clearForm}>Reset</button>
      </form>
    );
  }
}

这是我尝试对其进行错误处理的完整app.js主文件:

class App extends Component {
  constructor() {
    super();
    this.state = {
      bills: [],total: 0,};
  }

  componentDidMount() {
    const dbRef = firebase.database().ref();

    // Event listener that watches the database
    dbRef.on("value",(snapshot) => {
      const firebaseData = snapshot.val();

      const billData = [];

      for (const bill in firebaseData) {
        billData.push({
          id: bill,name: firebaseData[bill].name,value: firebaseData[bill].result,});
      }
      this.setState({
        bills: billData,});
    });
  }

  handleClick = (event,billInfo) => {
    event.preventDefault();
    const dbRef = firebase.database().ref();

    const result =
      (billInfo.amount / billInfo.group) * (billInfo.tip / 100 + 1);

    result === NaN
      ? alert("Please fill all forms!")
      : dbRef.push({
          result: result.toFixed(2),});

    this.setState({
      total: result.toFixed(2),});
  };

  deleteBill = (billId) => {
    const dbRef = firebase.database().ref();

    dbRef.child(billId).remove();
  };

  render() {
    return (
      <div className="wrapper">
        <h1 className="logoName">Spl|tr</h1>
        <h3>Bill Splitting App</h3>

        <Form getTotal={this.handleClick} />
        <h2>${this.state.total}</h2>

        <h3>Previous Bills</h3>
        <Bills receipts={this.state.bills} delete={this.deleteBill} />
      </div>
    );
  }
}

这是Bills.js组件,该组件在页面上单击按钮时位于窗体下方的先前项目:

const Bills = (props) => {
  return (
    <ul>
      {props.receipts.map((bill) => {
        return (
          <li key={bill.id}>
            <p>{bill.name}</p>
            <p>${bill.value}</p>
            <button onClick={() => props.delete(bill.id)}>
              <i class="fas fa-times-circle"></i>
            </button>
          </li>
        );
      })}
    </ul>
  );
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)