You need to make your inputs controlled by passing the value you store in your state
then you just have to reset the state values and your component value resets.(您需要通过传递存储在state
的值来控制输入,然后只需重置状态值,然后重置组件值即可。)
check this sample below(在下面检查此样本)
handleInputChange = (e) => {
let { name, value } = e.target;
this.setState({
...this.state,
inputs: {
[name]: value
}
});
}(})
your component will now look like(您的组件现在看起来像)
<input name='fullName' value={this.state.inputs.fullName} onChange={this.handleInputChange} />
Your reset function will just clear the state and your input field will be empty since it's controlled via state
(您的重置功能只会清除状态,并且您的输入字段为空,因为它是通过state
控制的)
resetInputFields = () => {
this.setState({ inputs: {} })
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…