React - clearing an input value after form submit

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

提交回复
热议问题