{"id":27058,"date":"2024-11-24T12:18:44","date_gmt":"2024-11-24T04:18:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27058\/"},"modified":"2024-11-24T12:18:44","modified_gmt":"2024-11-24T04:18:44","slug":"react-18-%e4%b8%a5%e6%a0%bc%e6%a8%a1%e5%bc%8f%e4%b8%8b%ef%bc%8c%e7%b1%bb%e7%bb%84%e4%bb%b6%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0%e6%a8%a1%e6%8b%9f%e6%b8%b2%e6%9f%93%e7%9a%84%e9%a1%ba%e5%ba%8f%e9%97%ae-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27058\/","title":{"rendered":"React 18 \u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898\uff1a\u5982\u4f55\u907f\u514d\u7ec4\u4ef6\u5728\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u8bbf\u95ee\u5df2\u5378\u8f7d\u5b9e\u4f8b\u53d8\u91cf\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173115294247681.jpg\" class=\"aligncenter\" title=\"React 18 \u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898\uff1a\u5982\u4f55\u907f\u514d\u7ec4\u4ef6\u5728\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u8bbf\u95ee\u5df2\u5378\u8f7d\u5b9e\u4f8b\u53d8\u91cf\uff1f\u63d2\u56fe\" alt=\"React 18 \u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898\uff1a\u5982\u4f55\u907f\u514d\u7ec4\u4ef6\u5728\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u8bbf\u95ee\u5df2\u5378\u8f7d\u5b9e\u4f8b\u53d8\u91cf\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>react 18 \u4e25\u683c\u6a21\u5f0f\u4e0b\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898<\/strong><\/p>\n<p>React 18 \u4e2d\uff0c\u5904\u4e8e\u4e25\u683c\u6a21\u5f0f\u4e0b\u7684\u7c7b\u7ec4\u4ef6\u4f1a\u6267\u884c\u6a21\u62df\u6e32\u67d3\uff0c\u5176\u751f\u547d\u5468\u671f\u4e0e\u901a\u5e38\u987a\u5e8f\u4e0d\u540c\uff1a<\/p>\n<ul>\n<li>constructor -&gt; \u6a21\u62df constructor -&gt; componentDidMount<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u5dee\u5f02\u7684\u4e00\u4e2a\u539f\u56e0\u662f\uff0c\u5728 React 18 \u4e2d\uff0c\u7c7b\u7ec4\u4ef6\u73b0\u5728\u901a\u8fc7 React.memo \u5305\u88c5\uff0cReact.memo \u5145\u5f53\u4e00\u4e2a\u8bb0\u5fc6\u51fd\u6570\uff0c\u5b83\u6bd4\u8f83 props \u7684\u53d8\u5316\u5e76\u51b3\u5b9a\u662f\u5426\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5b83\u4f1a\u5728\u6700\u521d\u6e32\u67d3\u4e4b\u524d\u6267\u884c\u4e00\u6b21\u6a21\u62df\u6e32\u67d3\uff0c\u8be5\u6a21\u62df\u6e32\u67d3\u4e0d\u4f1a\u89e6\u53d1 componentDidMount\u3002<\/p>\n<p>\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u56e0\u4e3a\u7ec4\u4ef6\u5728\u5176\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u5c06\u610f\u5916\u5730\u8bbf\u95ee\u5df2\u5378\u8f7d\u7ec4\u4ef6\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>class App extends React.Component {\n  constructor() {\n    this.state = { value: 0 };\n  }\n\n  componentDidMount() {\n    this.setState({ value: 1 });\n  }\n\n  render() {\n    return &lt;div&gt;{this.state.value}&lt;\/div&gt;;\n  }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7ec4\u4ef6\u5728\u6a21\u62df\u6e32\u67d3\u4e2d\u6784\u9020\u4e86 state\uff0c\u5e76\u5728 componentDidMount \u4e2d\u66f4\u65b0\u4e86 state\u3002\u7136\u800c\uff0c\u7531\u4e8e\u6a21\u62df\u6e32\u67d3\u4e0d\u662f\u6b63\u5f0f\u6e32\u67d3\uff0c\u56e0\u6b64 state \u66f4\u65b0\u4e0d\u4f1a\u88ab\u5e94\u7528\uff0c\u5e76\u4e14\u7ec4\u4ef6\u5c06\u5448\u73b0 0\uff0c\u800c\u4e0d\u662f 1\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u51e0\u79cd\u9009\u62e9\uff1a<\/p>\n<ul>\n<li>\u5c06\u521d\u59cb\u5316\u903b\u8f91\u79fb\u52a8\u5230 componentDidMount<\/li>\n<li>\u4f7f\u7528 React 18 \u7684\u65b0\u751f\u547d\u5468\u671f\u65b9\u6cd5\uff0c\u5982 getDerivedStateFromProps \u6216 getSnapshotBeforeUpdate\uff0c\u5b83\u4eec\u5728\u6bcf\u6b21\u6e32\u67d3\u524d\u90fd\u8c03\u7528<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fReact 18 \u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898\uff1a\u5982\u4f55\u907f\u514d\u7ec4\u4ef6\u5728\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u8bbf\u95ee\u5df2\u5378\u8f7d\u5b9e\u4f8b\u53d8\u91cf\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 \u4e25\u683c\u6a21\u5f0f\u4e0b\u7c7b\u7ec4\u4ef6\u6784\u9020\u51fd\u6570\u6a21\u62df\u6e32\u67d3\u7684\u987a\u5e8f\u95ee\u9898 React 18 \u4e2d\uff0c\u5904\u4e8e\u4e25\u683c\u6a21\u5f0f\u4e0b\u7684\u7c7b\u7ec4\u4ef6\u4f1a\u6267\u884c\u6a21\u62df\u6e32\u67d3\uff0c\u5176\u751f\u547d\u5468\u671f\u4e0e\u901a\u5e38\u987a\u5e8f\u4e0d\u540c\uff1a constructor -&gt; \u6a21\u62df constructor -&gt; componentDidMount \u8fd9\u79cd\u5dee\u5f02\u7684\u4e00\u4e2a\u539f\u56e0\u662f\uff0c\u5728 React 18 \u4e2d\uff0c\u7c7b\u7ec4\u4ef6\u73b0\u5728\u901a\u8fc7 React.memo \u5305\u88c5\uff0cReact.memo \u5145\u5f53\u4e00\u4e2a\u8bb0\u5fc6\u51fd\u6570\uff0c\u5b83\u6bd4\u8f83 props \u7684\u53d8\u5316\u5e76\u51b3\u5b9a\u662f\u5426\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5b83\u4f1a\u5728\u6700\u521d\u6e32\u67d3\u4e4b\u524d\u6267\u884c\u4e00\u6b21\u6a21\u62df\u6e32\u67d3\uff0c\u8be5\u6a21\u62df\u6e32\u67d3\u4e0d\u4f1a\u89e6\u53d1 componentDidMount\u3002 \u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u56e0\u4e3a\u7ec4\u4ef6\u5728\u5176\u7b2c\u4e00\u6b21\u5b9e\u9645\u6e32\u67d3\u65f6\u5c06\u610f\u5916\u5730\u8bbf\u95ee\u5df2\u5378\u8f7d\u7ec4\u4ef6\u7684\u5b9e\u4f8b\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a class App extends React.Component { constructor() { this.state = { value: 0 }; } componentDidMount() { this.setState({ value: 1 }); } render() { return &lt;div&gt;{this.state.value}&lt;\/div&gt;; } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7ec4\u4ef6\u5728\u6a21\u62df\u6e32\u67d3\u4e2d\u6784\u9020\u4e86 state\uff0c\u5e76\u5728 componentDidMount \u4e2d\u66f4\u65b0\u4e86 [&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-27058","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27058","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=27058"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27058\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27058"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27058"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27058"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}