antdv3 resetFields
时间: 2023-12-01 13:05:11 浏览: 57
The resetFields method in antd v3 is used to reset the values of all fields in a form to their initial values. This method can be called on the form instance and it has the following syntax:
```javascript
form.resetFields()
```
Here is an example of how to use the resetFields method:
```javascript
import React from 'react';
import { Form, Input, Button } from 'antd';
const FormItem = Form.Item;
class MyForm extends React.Component {
formRef = React.createRef();
onFinish = (values) => {
console.log('Received values of form: ', values);
};
handleReset = () => {
this.formRef.current.resetFields();
};
render() {
return (
<Form ref={this.formRef} onFinish={this.onFinish}>
<FormItem name="name" label="Name">
<Input />
</FormItem>
<FormItem>
<Button type="primary" htmlType="submit">
Submit
</Button>
<Button onClick={this.handleReset}>Reset</Button>
</FormItem>
</Form>
);
}
}
export default MyForm;
```
In this example, we create a form with a single input field and two buttons: one for submitting the form and another for resetting the form. The handleReset method is called when the reset button is clicked, which in turn calls the resetFields method on the form instance, resetting the input field to its initial value.
阅读全文