Clear and reset form input fields

前端 未结 12 1844
小蘑菇
小蘑菇 2020-11-28 22:36

I have a form containing various input fields and two buttons; one for submitting and one for cancelling.

相关标签:
12条回答
  • 2020-11-28 23:17

    You can also do it by targeting the current input, with anything.target.reset() . This is the most easiest way!

    handleSubmit(e){
     e.preventDefault();
     e.target.reset();
    }
    
    <form onSubmit={this.handleSubmit}>
      ...
    </form>
    
    0 讨论(0)
  • 2020-11-28 23:25

    I don't know if this is still relevant. But when I had similar issue this is how I resolved it.

    Where you need to clear an uncontrolled form you simply do this after submission.

    this.<ref-name-goes-here>.setState({value: ''});
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 23:30

    Using event.target.reset() only works for uncontrolled components, which is not recommended. For controlled components you would do something like this:

    import React, { Component } from 'react'
    
    class MyForm extends Component {
      initialState = { name: '' }
    
      state = this.initialState
    
      handleFormReset = () => {
        this.setState(() => this.initialState)
      }
    
      render() {
    
        return (
          <form onReset={this.handleFormReset}>
            <div>
              <label htmlFor="name">Name</label>
              <input
                type="text"
                placeholder="Enter name"
                name="name"
                value={name}
                onChange={this.handleInputOnChange}
              />
            </div>
            <div>
              <input
                type="submit"
                value="Submit"
              />
              <input
                type="reset"
                value="Reset"
              />
            </div>
          </form>
        )
      }
    }
    
    ContactAdd.propTypes = {}
    
    export default MyForm
    
    0 讨论(0)
  • 2020-11-28 23:30

    Following code should reset the form in one click.

    import React, { Component } from 'react';
    
    class App extends Component {
        constructor(props){
        	super(props);
        	this.handleSubmit=this.handleSubmit.bind(this);
        }
        handleSubmit(e){
        this.refs.form.reset();
        }
        render(){
            return(
            <div>
            	<form onSubmit={this.handleSubmit} ref="form">
                    <input type="text" placeholder="First Name!" ref='firstName'/><br/<br/>
                	<input type="text" placeholder="Last Name!" ref='lastName'/><br/><br/>
                    <button type="submit" >submit</button>
                </form>
           </div>
        }
    }

    0 讨论(0)
  • 2020-11-28 23:33

    This one works best to reset the form.

    import React, { Component } from 'react'
    class MyComponent extends Component {
      constructor(props){
        super(props)
        this.state = {
          inputVal: props.inputValue
        }
        // preserve the initial state in a new object
        this.baseState = this.state ///>>>>>>>>> note this one.
      }
      resetForm = () => {
        this.setState(this.baseState) ///>>>>>>>>> note this one.
      }
      submitForm = () => {
        // submit the form logic
      }
      updateInput = val => this.setState({ inputVal: val })
      render() {
        return (
          <form>
            <input
              onChange={this.updateInput}
              type="text
              value={this.state.inputVal} />
            <button
              onClick={this.resetForm}
              type="button">Cancel</button>
            <button
              onClick={this.submitForm}
              type="submit">Submit</button>
          </form>
        )
      }
    }
    
    0 讨论(0)
  • 2020-11-28 23:35
    state={ 
      name:"",
      email:""
    }
    
    handalSubmit = () => {
      after api call 
      let resetFrom = {}
      fetch('url')
      .then(function(response) {
        if(response.success){
           resetFrom{
              name:"",
              email:""
          }
        }
      })
      this.setState({...resetFrom})
    }
    
    0 讨论(0)
提交回复
热议问题