React - clearing an input value after form submit

前端 未结 6 1228
粉色の甜心
粉色の甜心 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: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: '' });
    }
    

提交回复
热议问题