--- order: 2 title: zh-CN: 表单方法调用(Class component) en-US: Form methods (Class component) --- ## zh-CN 我们推荐使用 `Form.useForm` 创建表单数据域进行控制。如果是在 class component 下,你也可以通过 `ref` 获取数据域。 ## en-US We recommend use `Form.useForm` to create data control. If you are using class component, you can get it by `ref`. ```tsx import { Button, Form, Input, Select } from 'antd'; import type { FormInstance } from 'antd/es/form'; import React from 'react'; const { Option } = Select; const layout = { labelCol: { span: 8 }, wrapperCol: { span: 16 }, }; const tailLayout = { wrapperCol: { offset: 8, span: 16 }, }; class App extends React.Component { formRef = React.createRef(); onGenderChange = (value: string) => { switch (value) { case 'male': this.formRef.current!.setFieldsValue({ note: 'Hi, man!' }); return; case 'female': this.formRef.current!.setFieldsValue({ note: 'Hi, lady!' }); return; case 'other': this.formRef.current!.setFieldsValue({ note: 'Hi there!' }); } }; onFinish = (values: any) => { console.log(values); }; onReset = () => { this.formRef.current!.resetFields(); }; onFill = () => { this.formRef.current!.setFieldsValue({ note: 'Hello world!', gender: 'male', }); }; render() { return (
prevValues.gender !== currentValues.gender} > {({ getFieldValue }) => getFieldValue('gender') === 'other' ? ( ) : null }
); } } export default App; ``` ```css #components-form-demo-control-ref .ant-btn { margin-right: 8px; } ```