简体   繁体   English

使用 React Hooks 提交时将表单字段重置为初始状态

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM