简体   繁体   English

清除 React Form 的正确方法是什么?

[英]What is the proper way to clear a React Form?

I have a form that has a few different type of elements (textbox, checkbox, etc.) that was created using Reactjs. I have been having a hard time trying to figure out how to clear a form.我有一个表单,其中包含一些使用 Reactjs 创建的不同类型的元素(文本框、复选框等)。我一直很难弄清楚如何清除表单。 I've googled and not really sure of those solutions.我在谷歌上搜索过,但不太确定这些解决方案。 I tried something like the following but found it was of no use.我尝试了类似下面的东西,但发现它没有用。

What I want to happen is if the user fills out the form and decides to clear the form, once they click 'Clear Form', the form should reset.我想要发生的是,如果用户填写表格并决定清除表格,一旦他们点击“清除表格”,表格应该重置。 All the fields need to be blank again.所有字段都需要再次留空。

  handleClearForm(){
    this.setState({
      decisionDate: '',
      Veggies:'',
      fullName:'',
      comment:''
    }) 
  }

How can I clear a form in react?如何清除反应中的表格? Any help would be much appreciated.任何帮助将非常感激。

Code代码

Check this improved code检查这个改进的代码

 class App extends React.Component{
  constructor(){
    super()
    this.state = {
      decisionDate: '',
      Veggies:'',
      fullName:'',
      comment:''
    }
  }
  
    
    setdecisionDateValue (value) {
      this.setState({
        decisionDate: value
      })
    }
  
  componentDidMount(){
    $( "#decisionDate" ).datepicker({
      onSelect: this.setdecisionDateValue
    });
  }
  
  handleClearForm = () => {
    this.setState({
      decisionDate: '',
      Veggies:'',
      fullName:'',
      comment:''
    }) 
  }
  
  handleChange = (e) => {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;
    
    this.setState({[name]: value})
  }
  
  render(){
    const { decisionDate,Veggies,fullName,comment} = this.state;
    return(
     <div>
        <input type="text" name="fullName" 
        placeholder="Full Name" 
        onChange={this.handleChange} 
        value={fullName}/><br /><br />
        <input type="text" 
         name="decisionDate" 
         id="decisionDate"
         onChange={this.handleChange} 
         placeholder="MM/DD/YYYY" 
         value={this.state.decisionDate} /><br /><br />
        <textarea name="comment" 
         value={comment} 
         onChange={this.handleChange}/><br /><br />
        <input 
          type="checkbox"
          name="Veggies"
          onChange={this.handleChange} 
          checked={Veggies}
          />Veggies<br /><br />
        <button onClick={this.handleClearForm}>
         Clear Form
        </button>
      </div>
    )
  }
}

ReactDOM.render(
 <App />,
  document.getElementById("root")
)

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

相关问题 使用 Jest 和 react-testing-library 测试具有大量输入字段的表单的正确方法是什么? - What is the proper way to test a form with a lot of input fields using Jest and react-testing-library? 在 React 中使用 Hooks 获取 JSON 的正确方法是什么? - What is the proper way to fetch JSON in React with Hooks? React Components - 创建它们的正确方法是什么? - React Components - What is the proper way to create them? 使用 React Hooks 设置 state 的正确方法是什么? - What is the proper way to set state with React Hooks? 构建React 0.12项目的正确方法是什么? - What is the proper way to build a React 0.12 Project? 删除 React 元素的正确方法是什么? - What is the proper way to delete a React element? 创建可重用组件的正确方法是什么? - What is the proper way to create reusable components react 使表单在无状态的React组件中提交数据的正确方法是什么? - Whats the proper way to have a form submit data in a stateless react component? 使用无状态组件实现表单输入验证的正确方法是什么 - What is the proper way of implementing form input validation with stateless components 从ExtJS 4中的表单中卸载/取消记录的正确方法是什么? - What is the proper way to unload/unbind a record from a form in ExtJS 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM