我想在react中单击提交按钮后重置文本区域。我可以将状态重置为默认值,但是文本区域不会清除。任何想法?
下面是我的代码:class Setting extends Component {
constructor(){
this.state ={
input: null
}
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ input: event.target.value });
}
handleSubmit(event) {
//here is some logic to for handling input
// .....
event.preventDefault();
// here I want to reset the textarea content once the logic is done
this.restInput();
}
restInput= () => {
this.setState({
input :null
})
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<textarea value={this.state.input} onChange={this.handleChange} />
<input type="submit" value="Submit" />
</form>
)
}
}
将null
更改为空字符串,它将正常工作
restInput= () => {
this.setState({
input :""
})
}