【问题标题】:How do I clear the input values in a react form?如何清除反应表单中的输入值?
【发布时间】:2018-02-10 14:36:06
【问题描述】:

在我的代码中,我在 onSubmit 事件中提到了 preventDefault 方法,因此当用户在表单中输入数据时我的页面没有重新加载...

你能告诉我有什么方法可以将数据导出到 firebase 并自动重新加载我的页面!

代码如下:

import React, { Component } from 'react';
const firebase = require('firebase');
const uuid = require('uuid');

var config = {
  apiKey: "AIzaSyAtpOSiCqFy43ZTE-7CJdcHrIGNN1GrsSk",
  authDomain: "electronic-health-record-a795c.firebaseapp.com",
  databaseURL: "https://electronic-health-record-a795c.firebaseio.com",
  projectId: "electronic-health-record-a795c",
  storageBucket: "electronic-health-record-a795c.appspot.com",
  messagingSenderId: "545743770560"
};
firebase.initializeApp(config);
class Usurvey extends Component {
  constructor(props) {
    super(props);
    this.state = {
      uid: uuid.v1(),
      firstName: '',
      lastName: '',
    };
    this.submitData = this.submitData.bind(this);
    this.inputData = this.inputData.bind(this);
  }

  componentDidMount() {
    firebase
      .database()
      .ref(`Newdata/${this.state.uid}`)
      .on('value', snap => console.log('from db', snap.val()));
  }

  submitData(event) {
    event.preventDefault();
    firebase
      .database()
      .ref(`Newdata/${this.state.uid}`)
      .set({
        firstName: this.state.firstName,
        lastName: this.state.lastName,
      })
      .catch(error => console.log(error));
  }
  inputData(event) {
    const firstName = this.refs.name1.value;
    const lastName = this.refs.name2.value;
    this.setState({ firstName, lastName });
  }
  render() {
    return (
      <div>
        <form onSubmit={this.submitData}>
          <input type="text" onChange={this.inputData} ref="name1" />
          <input type="text" onChange={this.inputData} ref="name2" />
          <input type="submit" />Submit
        </form>
      </div>
    );
  }
}
export default Usurvey;

【问题讨论】:

  • 一开始为什么要重新加载页面,SPA的整个概念就是在不重新加载页面的情况下做事。
  • 那么用户如何输入详细信息并再次访问该页面?
  • 您应该在提交时重置您的表单,这样用户可以根据需要输入更多信息。此外,您使用的 refs 方法也不是更改表单的最佳方法。你应该更新你的状态,但不是这样。
  • 看看我的回答。
  • 是的,它完全有效。谢谢你们俩。

标签: forms reactjs firebase input


【解决方案1】:

应该是这样的

 class Usurvey extends Component {
   constructor(props) {
    super(props);
    this.state = {
      uid: uuid.v1(),
      firstName: '',
      lastName: '',
    };
    this.submitData = this.submitData.bind(this);
    this.inputData = this.inputData.bind(this);
  }

   componentDidMount() {
     firebase
      .database()
      .ref(`Newdata/${this.state.uid}`)
      .on('value', snap => console.log('from db', snap.val()));
   }

   submitData(e) {
    const { firstName, lastName } = this.state;
    e.preventDefault();
    firebase
     .database()
     .ref(`Newdata/${this.state.uid}`)
     .set({
       firstName: firstName,
       lastName: lastName,
     })
     .catch(error => console.log(error));

      this.setState({
        firstName: '', lastName: ''
      });
   }

   inputData(e) {
     this.setState({ [e.target.name]: e.target.value });
   }

   render() {
     return (
       <div>
         <form onSubmit={this.submitData}>
          <input type="text" value={this.state.firstName} onChange={this.inputData} name="firstName" />
          <input type="text" value={this.state.lastName} onChange={this.inputData} name="lastName" />
          <button type="submit">Submit</button>
         </form>
        </div>
     );
   }
}
export default Usurvey;

当用户在表单中输入时,您可以直接更新您的状态,同时在您提交时重置您的表单,以便用户可以在表单中添加更多信息。

【讨论】:

  • const { firstName, lastName } this.state;上面的行显示错误.....!
  • 这是一个错字,我已经改正了。
  • 我尝试了您的解决方案,但仍然在提交时字段不是空白,它们保留了它们的值。
  • 哦,我的错!很抱歉,问题出在&lt;input /&gt; 字段中。我没有给它 value 字段并将该道具绑定到该组件状态。现在它应该可以工作了。
【解决方案2】:

如果您想上传数据并在完成后重新加载,您可以这样做:

submitData(event) {
    event.preventDefault();
    firebase
      .database()
      .ref(`Newdata/${this.state.uid}`)
      .set({
        firstName: this.state.firstName,
        lastName: this.state.lastName,
      })
      .then(() => {
        location.reload();
        // or window.location.reload() if location is undefined
      })
      .catch(error => console.log(error));
  }

【讨论】:

  • 我不喜欢这种方法,但如果location.reload() 不起作用。那么这将window.location.reload() 不要使用this
  • 已修复,抱歉
猜你喜欢
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
相关资源
最近更新 更多