React - clearing an input value after form submit

前端 未结 6 1224
粉色の甜心
粉色の甜心 2020-12-05 04:09

I\'m presented with a rather silly problem. I am in the process of creating my first React application and I have encountered a little issue, where I am not able to clear my

相关标签:
6条回答
  • 2020-12-05 04:40

    This is the value that i want to clear and create it in state 1st STEP

    state={
    TemplateCode:"",
    }
    

    craete submitHandler function for Button or what you want 3rd STEP

    submitHandler=()=>{
    this.clear();//this is function i made
    }
    

    This is clear function Final STEP

    clear = () =>{
      this.setState({
        TemplateCode: ""//simply you can clear Templatecode
      });
    }
    

    when click button Templatecode is clear 2nd STEP

    <div class="col-md-12" align="right">
      <button id="" type="submit" class="btn btnprimary" onClick{this.submitHandler}> Save 
      </button>
    </div>
    
    0 讨论(0)
  • 2020-12-05 04:42

    In your onHandleSubmit function, set your state to {city: ''} again like this :

    this.setState({ city: '' });
    
    0 讨论(0)
  • 2020-12-05 04:47

    You are having a controlled component where input value is determined by this.state.city. So once you submit you have to clear your state which will clear your input automatically.

    onHandleSubmit(e) {
        e.preventDefault();
        const city = this.state.city;
        this.props.onSearchTermChange(city);
        this.setState({
          city: ''
        });
    }
    
    0 讨论(0)
  • 2020-12-05 04:53

    The answers above are incorrect, they will all run weather or not the submission is successful... You need to write an error component that will receive any errors then check if there are errors in state, if there are not then clear the form....

    use .then()

    example:

     const onSubmit =  e => {
    e.preventDefault();
    const fd = new FormData();
    fd.append("ticketType", ticketType);
    fd.append("ticketSubject", ticketSubject);
    fd.append("ticketDescription", ticketDescription);
    fd.append("itHelpType", itHelpType);
    fd.append("ticketPriority", ticketPriority);
    fd.append("ticketAttachments", ticketAttachments);
    newTicketITTicket(fd).then(()=>{
      setTicketData({
        ticketType: "IT",
        ticketSubject: "",
        ticketDescription: "",
        itHelpType: "",
        ticketPriority: ""
      })
    })  
    

    };

    0 讨论(0)
  • 2020-12-05 04:54

    this.mainInput doesn't actually point to anything. Since you are using a controlled component (i.e. the value of the input is obtained from state) you can set this.state.city to null:

    onHandleSubmit(e) {
      e.preventDefault();
      const city = this.state.city;
      this.props.onSearchTermChange(city);
      this.setState({ city: '' });
    }
    
    0 讨论(0)
  • 2020-12-05 05:02

    Since you input field is a controlled element, you cannot directly change the input field value without modifying the state.

    Also in

    onHandleSubmit(e) {
        e.preventDefault();
        const city = this.state.city;
        this.props.onSearchTermChange(city);
        this.mainInput.value = "";
      }
    

    this.mainInput doesn't refer the input since mainInput is an id, you need to specify a ref to the input

    <input
          ref={(ref) => this.mainInput= ref}
          onChange={this.onHandleChange}
          placeholder="Get current weather..."
          value={this.state.city}
          type="text"
        />
    

    In you current state the best way is to clear the state to clear the input value

    onHandleSubmit(e) {
        e.preventDefault();
        const city = this.state.city;
        this.props.onSearchTermChange(city);
        this.setState({city: ""});
      }
    

    However if you still for some reason want to keep the value in state even if the form is submitted, you would rather make the input uncontrolled

    <input
          id="mainInput"
          onChange={this.onHandleChange}
          placeholder="Get current weather..."
          type="text"
        />
    

    and update the value in state onChange and onSubmit clear the input using ref

     onHandleChange(e) {
        this.setState({
          city: e.target.value
        });
      }
    
      onHandleSubmit(e) {
        e.preventDefault();
        const city = this.state.city;
        this.props.onSearchTermChange(city);
        this.mainInput.value = "";
      }
    

    Having Said that, I don't see any point in keeping the state unchanged, so the first option should be the way to go.

    0 讨论(0)
提交回复
热议问题