{"id":351,"date":"2026-04-12T20:09:00","date_gmt":"2026-04-12T12:09:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=351"},"modified":"2026-06-12T21:35:56","modified_gmt":"2026-06-12T13:35:56","slug":"%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e7%94%a8useeffect","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/04\/12\/%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e7%94%a8useeffect\/","title":{"rendered":"\u4ec0\u4e48\u65f6\u5019\u7528useEffect"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import React, { useState } from 'react';\nimport AccountLogin from '.\/component\/AccountLogin.jsx'\nimport SmCodeLogin from '.\/component\/SmCodeLogin.jsx'\nimport { Form, Input, Button, Row, Col } from 'antd';\nimport IconMap from 'components\/IconMap.jsx';\nimport logoImg from 'common\/img\/logo.png';\nimport '.\/css\/login.less';\n\nconst FormItem = Form.Item;\nconst Login = () => {\n    const &#91;form] = Form.useForm();\n    const &#91;type, setType] = useState('account');\n    console.log(form,'form');\n    const submitUserInfo = (values) => {\n        console.log(values);\n    }\n    \n    const ComponentSelector = props => type==='account' ? &lt;AccountLogin {...props} \/> : &lt;SmCodeLogin {...props} \/>;\n\n    return (\n        &lt;div className=\"form-wrapper\">\n            &lt;div className=\"logo\">\n                &lt;img src={logoImg} alt=\"logo\" \/>\n                &lt;span>\u4eba\u4e8b\u7ba1\u7406\u7cfb\u7edf&lt;\/span>\n            &lt;\/div>\n            &lt;Form form={form} onFinish={submitUserInfo}>\n                {ComponentSelector({form, FormItem, Input})}\n                &lt;Row>\n                    &lt;Button block={true} type=\"primary\" htmlType=\"submit\">\u767b\u5f55&lt;\/Button>\n                &lt;\/Row>\n                &lt;Row className=\"txt-tip\">\n                    &lt;Col span={6}>\u5fd8\u8bb0\u5bc6\u7801&lt;\/Col>\n                    &lt;Col span={18} className=\"txt-right\" onClick={() => setType(type!=='account' ? 'account' : 'smcode')}>\n                    {\n                        type!=='account' ? '\u4f7f\u7528\u8d26\u6237\u540d\u5bc6\u7801\u8fdb\u884c\u767b\u5f55' : '\u4f7f\u7528\u624b\u673a\u53f7\u8fdb\u884c\u767b\u5f55'\n                    }\n                    {IconMap.arrRowRight}\n                    &lt;\/Col>\n                &lt;\/Row>\n            &lt;\/Form>\n        &lt;\/div>\n    );\n};\nexport default Login;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">1\u3001\u4e3a\u4ec0\u4e48\u4e0d\u7528 useEffect\uff1f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u8fd9\u6bb5\u662f<strong>JSX \u6761\u4ef6\u6e32\u67d3<\/strong>\uff0c\u5904\u5728\u7ec4\u4ef6\u6e32\u67d3\u6267\u884c\u6d41\u7a0b\u91cc\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u7ec4\u4ef6\u4efb\u610f state \/props \u66f4\u65b0 \u2192 \u51fd\u6570\u7ec4\u4ef6\u91cd\u65b0\u6267\u884c\uff1b<\/li>\n\n\n\n<li><code>type<\/code> \u662f <code>useState<\/code> \u53d8\u91cf\uff0c\u8c03\u7528 <code>setType<\/code> \u4fee\u6539\u540e\uff0c\u7ec4\u4ef6\u91cd\u6e32\u67d3\uff1b<\/li>\n\n\n\n<li>\u4e09\u5143\u8868\u8fbe\u5f0f\u91cd\u65b0\u5224\u65ad <code>type === 'account'<\/code>\uff1b<\/li>\n\n\n\n<li>\u8981\u4e48\u8fd4\u56de <code>&lt;AccountLogin><\/code>\uff0c\u8981\u4e48\u8fd4\u56de <code>&lt;SmCodeLogin><\/code>\uff0cDOM \u81ea\u52a8\u66ff\u6362\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\u5b8c\u6574\u4ee3\u7801\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;type, setType] = useState('account');\n\nconst ComponentSelector = props =&gt; {\n  return type === 'account'\n    ? &lt;AccountLogin {...props} \/&gt;\n    : &lt;SmCodeLogin {...props} \/&gt;;\n};\n\n\/\/ \u4f7f\u7528\n&lt;ComponentSelector \/&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>setType('code')<\/code> \u6267\u884c\u540e\u7acb\u523b\u6362\u7ec4\u4ef6\uff0c\u5b8c\u5168\u4e0d\u9700\u8981\u624b\u52a8\u76d1\u542c <code>type<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u4e24\u4e2a\u7ec6\u8282\u5173\u952e\u70b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\uff081\uff09\u7ec4\u4ef6\u4f1a\u5378\u8f7d\u91cd\u5efa\uff0c\u5185\u90e8\u72b6\u6001\u4f1a\u6e05\u7a7a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AccountLogin<\/code> \u548c <code>SmCodeLogin<\/code> \u662f\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5207\u6362\u65f6\u65e7\u7ec4\u4ef6\u6267\u884c <code>useEffect(()=>()=>{})<\/code> \u5378\u8f7d\u903b\u8f91\uff1b<\/li>\n\n\n\n<li>\u65b0\u7ec4\u4ef6\u5168\u65b0\u6302\u8f7d\uff0c\u5185\u90e8 <code>useState<\/code>\u3001\u8f93\u5165\u6846\u5185\u5bb9\u90fd\u4f1a\u91cd\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5e0c\u671b<strong>\u5207\u6362\u7c7b\u578b\u4fdd\u7559\u8868\u5355\u8f93\u5165\u5185\u5bb9<\/strong>\uff0c\u5c31\u4e0d\u80fd\u76f4\u63a5\u66ff\u6362\u7ec4\u4ef6\uff0c\u8981\u7528\u663e\u793a \/ \u9690\u85cf\uff08<code>display:none<\/code>\uff09\u800c\u975e\u6761\u4ef6\u5378\u8f7d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\uff082\uff09\u4ec0\u4e48\u65f6\u5019\u624d\u771f\u7684\u9700\u8981 useEffect\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u6709\u4e0b\u9762\u8fd9\u7c7b\u573a\u666f\u624d\u8981\u76d1\u542c <code>type<\/code>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>type \u53d8\u5316\u65f6\uff0c\u53d1\u63a5\u53e3\u8bf7\u6c42\uff1b<\/li>\n\n\n\n<li>type \u53d8\u5316\u65f6\u91cd\u7f6e\u5916\u5c42 form \u8868\u5355\uff1b<\/li>\n\n\n\n<li>type \u53d8\u5316\u64cd\u4f5c DOM\u3001\u6267\u884c\u5f02\u6b65\u903b\u8f91\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">jsx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useEffect(() =&gt; {\n  \/\/ \u7c7b\u578b\u5207\u6362\uff0c\u6e05\u7a7a\u8868\u5355\n  form.resetFields();\n}, &#91;type]);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ea\u662f\u5355\u7eaf\u5207\u6362\u6e32\u67d3\u54ea\u4e2a\u7ec4\u4ef6\uff0c\u5b8c\u5168\u6ca1\u5fc5\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4e3a\u4ec0\u4e48\u4e0d\u7528 useEffect\uff1f \u4f60\u8fd9\u6bb5\u662fJS..<\/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":[28],"class_list":["post-351","post","type-post","status-publish","format-standard","hentry","category-react-","category-front","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\/351","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=351"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/351\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}