{"id":349,"date":"2026-04-12T20:06:00","date_gmt":"2026-04-12T12:06:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=349"},"modified":"2026-06-12T20:09:51","modified_gmt":"2026-06-12T12:09:51","slug":"ant-design-form-%e7%9a%84-form-useform","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/04\/12\/ant-design-form-%e7%9a%84-form-useform\/","title":{"rendered":"Ant Design Form \u7684 Form.useForm()"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u5148\u8bf4\u6838\u5fc3\uff1a\u4e0d\u7528 useForm \u539f\u751f\u4e5f\u80fd\u505a\u8868\u5355\uff0c\u4f46\u6781\u5176\u9ebb\u70e6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u751f React \u7ba1\u63a7\u8868\u5355\u601d\u8def\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bcf\u4e00\u4e2a\u8f93\u5165\u6846\u5355\u72ec\u7ed1\u5b9a <code>value<\/code> + <code>onChange<\/code>\uff0c\u81ea\u5df1\u58f0\u660e\u4e00\u5806 state\u3001\u624b\u5199\u53d6\u503c\u3001\u6821\u9a8c\u3001\u91cd\u7f6e\u3001\u56de\u586b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u539f\u751f\u7b28\u5199\u6cd5\nconst &#91;username, setUsername] = useState('');\nconst &#91;password, setPassword] = useState('');\n\/\/ \u6bcf\u4e2aInput\u90fd\u8981\u7ed1onChange\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b57\u6bb5\u4e00\u591a\uff08\u5341\u51e0\u3001\u51e0\u5341\u4e2a\u8868\u5355\u9879\uff09\u4ee3\u7801\u7206\u70b8\u3001\u7ef4\u62a4\u707e\u96be\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ant Design Form \u7684 <code>Form.useForm()<\/code> \u5c31\u662f<strong>\u8868\u5355\u63a7\u5236\u5668\u5b9e\u4f8b<\/strong>\uff0c\u628a\u8868\u5355\u6240\u6709\u903b\u8f91\u7edf\u4e00\u6258\u7ba1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001useForm \u5b8c\u6574\u4f5c\u7528\u6e05\u5355<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u7edf\u4e00\u6536\u96c6\u6240\u6709\u8868\u5355\u5b57\u6bb5\u503c\uff0c\u4e0d\u7528\u9010\u4e2a\u7ed1\u5b9a state<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u7528 <code>form.validateFields()<\/code> \/ <code>form.getFieldsValue()<\/code> \u4e00\u6b21\u6027\u62ff\u5230\u5168\u90e8\u8868\u5355\u6570\u636e\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;form] = Form.useForm();\n\nconst onSubmit = async () => {\n  \/\/ \u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u8868\u5355\u503c\n  const values = await form.validateFields();\n  console.log(values.username, values.password);\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5185\u7f6e\u8868\u5355\u6821\u9a8c\uff0c\u4e0d\u7528\u81ea\u5df1\u5199\u6b63\u5219\u5224\u65ad<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;Form.Item name=\"username\" rules={[]}&gt;<\/code> \u914d\u7f6e\u6821\u9a8c\u89c4\u5219\uff0c\u63d0\u4ea4\u81ea\u52a8\u89e6\u53d1\u6821\u9a8c\uff0c\u9519\u8bef\u63d0\u793a\u81ea\u52a8\u6e32\u67d3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5fc5\u586b\u3001\u957f\u5ea6\u9650\u5236\u3001\u624b\u673a\u53f7\u3001\u90ae\u7bb1\u3001\u81ea\u5b9a\u4e49\u6821\u9a8c \u6821\u9a8c\u5931\u8d25\u81ea\u52a8\u6807\u7ea2\u3001\u5c55\u793a\u6587\u6848\uff0c\u4e0d\u7528\u624b\u52a8\u5199 if \u5224\u65ad\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4e00\u952e\u91cd\u7f6e\u3001\u6e05\u7a7a\u8868\u5355<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>form.resetFields(); \/\/ \u5168\u90e8\u6062\u590d\u521d\u59cb\u503c<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u56de\u586b\u7f16\u8f91\u6570\u636e\uff08\u7f16\u8f91\u5f39\u7a97\u5fc5\u5907\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u8f91\u6253\u5f00\u5f39\u7a97\u65f6\uff0c\u540e\u7aef\u56de\u663e\u6570\u636e\u4e00\u884c\u4ee3\u7801\u8d4b\u503c\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>form.setFieldsValue(record);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u7528\u6328\u4e2a\u7ed9\u6bcf\u4e00\u4e2a input \u8d4b\u503c state\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5c40\u90e8\u66f4\u65b0\u5355\u4e2a\u5b57\u6bb5\u3001\u8bbe\u7f6e\u7981\u7528\u3001\u53ea\u8bfb\u3001\u52a8\u6001\u663e\u9690<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4fee\u6539\u5355\u4e2a\u5b57\u6bb5\u503c\nform.setFieldValue('username', 'admin');\n\/\/ \u83b7\u53d6\u5355\u4e2a\u5b57\u6bb5\nform.getFieldValue('password');\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u8868\u5355\u8054\u52a8\uff08A \u8f93\u5165\u6846\u53d8\u5316\uff0cB \u81ea\u52a8\u6539\u53d8\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u9700\u8981\u590d\u6742\u76d1\u542c useState \u53d8\u5316\uff0c\u4f9d\u6258 form \u5b9e\u4f8b\u5373\u53ef\u505a\u5b57\u6bb5\u8054\u52a8\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u8131\u79bb JSX \u7ec4\u4ef6\u5c42\u7ea7\u64cd\u4f5c\u8868\u5355<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">form \u5b9e\u4f8b\u53ef\u4ee5\u63d0\u5347\u5230\u7236\u7ec4\u4ef6\u3001\u5f39\u7a97\u5916\u90e8\uff0c\u8de8\u7ec4\u4ef6\u63a7\u5236\u5b50\u8868\u5355\uff1b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u7528\u9760\u5c42\u5c42\u4f20\u51fd\u6570\u3001\u56de\u8c03\u7236\u5b50\u901a\u4fe1\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7ecf\u5178\u6700\u7b80\u5b8c\u6574\u793a\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\nimport { Form, Input, Button } from 'antd';\n\nexport default function Login() {\n  \/\/ \u521b\u5efa\u8868\u5355\u63a7\u5236\u5668\u5b9e\u4f8b\n  const &#91;form] = Form.useForm();\n\n  \/\/ \u63d0\u4ea4\u56de\u8c03\uff0c\u6821\u9a8c\u901a\u8fc7\u81ea\u52a8\u63a5\u6536\u8868\u5355\u503c\n  const onFinish = (values) =&gt; {\n    console.log('\u8868\u5355\u6570\u636e\uff1a', values);\n  };\n\n  return (\n    &lt;Form form={form} onFinish={onFinish}&gt;\n      &lt;Form.Item name=\"username\" rules={&#91;{ required: true, message: '\u8bf7\u8f93\u5165\u8d26\u53f7' }]}&gt;\n        &lt;Input placeholder=\"\u8d26\u53f7\" \/&gt;\n      &lt;\/Form.Item&gt;\n      &lt;Form.Item name=\"password\" rules={&#91;{ required: true, message: '\u8bf7\u8f93\u5165\u5bc6\u7801' }]}&gt;\n        &lt;Input.Password placeholder=\"\u5bc6\u7801\" \/&gt;\n      &lt;\/Form.Item&gt;\n\n      &lt;Button type=\"primary\" htmlType=\"submit\"&gt;\u767b\u5f55&lt;\/Button&gt;\n      &lt;Button onClick={() =&gt; form.resetFields()}&gt;\u91cd\u7f6e&lt;\/Button&gt;\n    &lt;\/Form&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u8865\u5145\u5173\u952e\u7ec6\u8282<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>const [form] = Form.useForm()<\/code> \u751f\u6210\u72ec\u7acb\u8868\u5355\u5b9e\u4f8b\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a\u9875\u9762\u591a\u4e2a\u72ec\u7acb\u8868\u5355\uff0c\u8c03\u7528\u591a\u6b21 <code>useForm()<\/code> \u751f\u6210\u591a\u4e2a\u5b9e\u4f8b\u4e92\u4e0d\u5e72\u6270\uff1b<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u4e3a\u4ec0\u4e48\u8981\u628a form \u4f20\u7ed9 <code>&lt;Form form={form}><\/code>\uff1f JSX \u91cc\u7684 Form \u7ec4\u4ef6\u548c\u5916\u90e8 form \u5b9e\u4f8b\u5efa\u7acb\u53cc\u5411\u7ed1\u5b9a\uff0c\u5b9e\u4f8b\u624d\u80fd\u63a5\u7ba1\u5185\u90e8\u6240\u6709 Form.Item\uff1b<\/li>\n\n\n\n<li>\u53d7\u63a7\u4e0d\u518d\u4f9d\u8d56\u7ec4\u4ef6\u81ea\u8eab state \u6240\u6709\u5b57\u6bb5\u72b6\u6001\u3001\u6821\u9a8c\u72b6\u6001\u3001\u9519\u8bef\u4fe1\u606f\u90fd\u5b58\u5728 form \u5b9e\u4f8b\u5185\u90e8\uff0c\u7ec4\u4ef6\u65e0\u9700\u6279\u91cf\u58f0\u660e useState<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5148\u8bf4\u6838\u5fc3\uff1a\u4e0d\u7528 useForm \u539f\u751f\u4e5f\u80fd\u505a\u8868\u5355..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11],"tags":[31,28],"class_list":["post-349","post","type-post","status-publish","format-standard","hentry","category-react-","category-front","tag-ant-design","tag-react"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/react-%e5%89%8d%e7%ab%af\/\" rel=\"category tag\">React<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=349"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/349\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/349\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}