antdv3 resetFields
时间: 2023-12-01 14:05:09 浏览: 60
The `resetFields` method of Ant Design v3 is used to reset the values of one or more form fields to their initial state. This method can be called on the `Form` instance or on a `Form.Item` instance.
Here's an example of calling `resetFields` on a `Form` instance:
```jsx
import { Form, Input, Button } from 'antd';
const MyForm = () => {
const [form] = Form.useForm();
const handleReset = () => {
form.resetFields();
};
return (
<Form form={form}>
<Form.Item name="username" label="Username">
<Input />
</Form.Item>
<Form.Item name="password" label="Password">
<Input.Password />
</Form.Item>
<Button onClick={handleReset}>Reset</Button>
</Form>
);
};
```
In the above example, the `handleReset` function is called when the "Reset" button is clicked. This function calls `resetFields` on the `form` object, which resets the values of both the "username" and "password" fields to their initial state.
You can also pass an array of field names to `resetFields` to only reset specific fields:
```jsx
const handleReset = () => {
form.resetFields(['username']);
};
```
In this example, only the "username" field will be reset when the "Reset" button is clicked.
阅读全文