{"id":3350,"date":"2024-11-10T16:40:43","date_gmt":"2024-11-10T08:40:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3350\/"},"modified":"2024-11-10T16:40:43","modified_gmt":"2024-11-10T08:40:43","slug":"react-18-%e7%b1%bb%e7%bb%84%e4%bb%b6%e6%a8%a1%e6%8b%9f%e6%b8%b2%e6%9f%93%e4%b8%ad%ef%bc%8cconstructor-%e5%88%9d%e5%a7%8b%e5%8c%96%e7%9a%84%e5%af%b9%e8%b1%a1%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3350\/","title":{"rendered":"React 18 \u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u4e2d\uff0cconstructor \u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e3a\u4ec0\u4e48\u4f1a\u88ab\u91cd\u7f6e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173103236828272.jpg\" class=\"aligncenter\" title=\"React 18 \u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u4e2d\uff0cconstructor \u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e3a\u4ec0\u4e48\u4f1a\u88ab\u91cd\u7f6e\uff1f\u63d2\u56fe\" alt=\"React 18 \u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u4e2d\uff0cconstructor \u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e3a\u4ec0\u4e48\u4f1a\u88ab\u91cd\u7f6e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>react 18 \u4e2d\u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u884c\u4e3a\u5206\u6790<\/strong><\/p>\n<p>\u5728 React 18 \u7684\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u4f1a\u6267\u884c\u6a21\u62df\u6e32\u67d3\u4ee5\u68c0\u6d4b\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u7c7b\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u8c03\u7528\u987a\u5e8f\u6709\u6240\u53d8\u5316\u3002<\/p>\n<p><strong>\u6a21\u62df\u6e32\u67d3\u987a\u5e8f<\/strong><\/p>\n<ul>\n<li>constructor (\u6a21\u62df)<\/li>\n<li>constructor (\u5b9e\u9645)<\/li>\n<li>componentDidMount (\u6a21\u62df)<\/li>\n<li>componentWillUnmount (\u6a21\u62df)<\/li>\n<li>componentDidMount (\u5b9e\u9645)<\/li>\n<\/ul>\n<p><strong>\u95ee\u9898\u4ea7\u751f\u7684\u539f\u56e0<\/strong><\/p>\n<p>\u95ee\u9898\u7684\u5173\u952e\u5728\u4e8e\uff0c\u7ec4\u4ef6\u5378\u8f7d\u65f6\uff0c\u5728 constructor \u4e2d\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e5f\u4f1a\u88ab\u91cd\u7f6e\u3002\u8fd9\u662f\u56e0\u4e3a\u6a21\u62df\u6e32\u67d3\u4e2d\u7684 componentWillUnmount \u4f1a\u8c03\u7528\u5b9e\u9645\u7ec4\u4ef6\u7684\u5378\u8f7d\u94a9\u5b50\uff0c\u4ece\u800c\u5f71\u54cd\u5b9e\u9645\u7ec4\u4ef6\u7684\u72b6\u6001\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<pre>constructor(props) {\n  super(props);\n  this.ob = new A();\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u5378\u8f7d\u65f6\uff0c\u8be5\u5bf9\u8c61\u4f1a\u88ab\u91cd\u7f6e\u4e3a null\uff1a<\/p>\n<pre>componentWillUnmount() {\n  this.ob = null;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u7b2c\u4e8c\u6b21\u6e32\u67d3\u4e2d\uff0c\u5728 constructor \u4e2d\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u5df2\u88ab\u91cd\u7f6e\uff0c\u4ece\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li>\u5c06\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u8f6c\u79fb\u5230 componentDidMount \u4e2d\u3002<\/li>\n<li>\u4f7f\u7528 React \u7684\u65b0\u751f\u547d\u5468\u671f\u94a9\u5b50 getDerivedStateFromProps \u6216 getSnapshotBeforeUpdate\u3002\u8fd9\u4e9b\u94a9\u5b50\u4f1a\u5728\u6bcf\u6b21\u6e32\u67d3\u524d\u8c03\u7528\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b89\u5168\u5730\u521d\u59cb\u5316\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fReact 18 \u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u4e2d\uff0cconstructor \u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e3a\u4ec0\u4e48\u4f1a\u88ab\u91cd\u7f6e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>react 18 \u4e2d\u7c7b\u7ec4\u4ef6\u6a21\u62df\u6e32\u67d3\u884c\u4e3a\u5206\u6790 \u5728 React 18 \u7684\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u4f1a\u6267\u884c\u6a21\u62df\u6e32\u67d3\u4ee5\u68c0\u6d4b\u6f5c\u5728\u7684\u6027\u80fd\u95ee\u9898\u3002\u5728\u6b64\u8fc7\u7a0b\u4e2d\uff0c\u7c7b\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u8c03\u7528\u987a\u5e8f\u6709\u6240\u53d8\u5316\u3002 \u6a21\u62df\u6e32\u67d3\u987a\u5e8f constructor (\u6a21\u62df) constructor (\u5b9e\u9645) componentDidMount (\u6a21\u62df) componentWillUnmount (\u6a21\u62df) componentDidMount (\u5b9e\u9645) \u95ee\u9898\u4ea7\u751f\u7684\u539f\u56e0 \u95ee\u9898\u7684\u5173\u952e\u5728\u4e8e\uff0c\u7ec4\u4ef6\u5378\u8f7d\u65f6\uff0c\u5728 constructor \u4e2d\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u4e5f\u4f1a\u88ab\u91cd\u7f6e\u3002\u8fd9\u662f\u56e0\u4e3a\u6a21\u62df\u6e32\u67d3\u4e2d\u7684 componentWillUnmount \u4f1a\u8c03\u7528\u5b9e\u9645\u7ec4\u4ef6\u7684\u5378\u8f7d\u94a9\u5b50\uff0c\u4ece\u800c\u5f71\u54cd\u5b9e\u9645\u7ec4\u4ef6\u7684\u72b6\u6001\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728\u6784\u9020\u51fd\u6570\u4e2d\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff1a constructor(props) { super(props); this.ob = new A(); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u5378\u8f7d\u65f6\uff0c\u8be5\u5bf9\u8c61\u4f1a\u88ab\u91cd\u7f6e\u4e3a null\uff1a componentWillUnmount() { this.ob = null; } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u5bfc\u81f4\u7b2c\u4e8c\u6b21\u6e32\u67d3\u4e2d\uff0c\u5728 constructor \u4e2d\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u5df2\u88ab\u91cd\u7f6e\uff0c\u4ece\u800c\u5bfc\u81f4\u9519\u8bef\u3002 \u89e3\u51b3\u65b9\u6cd5 \u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff1a \u5c06\u5bf9\u8c61\u7684\u521d\u59cb\u5316\u8f6c\u79fb\u5230 componentDidMount \u4e2d\u3002 \u4f7f\u7528 React \u7684\u65b0\u751f\u547d\u5468\u671f\u94a9\u5b50 getDerivedStateFromProps [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3350","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3350"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3350\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}