[英]Reset form field to Initial State on submit with React Hooks
I'm trying to reset my form fields to blank when a button is pressed.我试图在按下按钮时将表单字段重置为空白。 For some reason its not working.
由于某种原因,它不起作用。 Here is my sample code:
这是我的示例代码:
initialize:初始化:
function App() {
const [textField, setTextField] = useState("");
Clear the state:清除状态:
const clearState = () => {
setTextField('');
}
On submit:提交时:
const submitForm = () => {
clearState();
};
Form field:表单域:
<Form.Group controlId="textField">
<Form.Control placeholder="Description" onChange={handleTextFieldInput} />
</Form.Group>
But when I submit, the value is still the same as was input.但是当我提交时,该值仍然与输入相同。
在Form.Control
上设置value={textField}
,应该可以解决它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.