{"id":52694,"date":"2024-12-03T13:58:37","date_gmt":"2024-12-03T05:58:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52694\/"},"modified":"2024-12-03T13:58:37","modified_gmt":"2024-12-03T05:58:37","slug":"react-%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e6%b8%b2%e6%9f%93%e6%80%a7%e8%83%bd-%e5%a4%87%e5%bf%98%e5%bd%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52694\/","title":{"rendered":"React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aReact \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<ul>\n<li>\u8fd9\u4e9b\u662f\u5b50\u7ec4\u4ef6\u5c06\u88ab\u6e32\u67d3\u7684\u6a21\u5f0f\u3002<\/li>\n<\/ul>\n<ol>\n<li>\n<p>\u5f53\u7236\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3\u65f6\uff0c\u4f8b\u5982\u66f4\u65b0\u81ea\u8eab\u72b6\u6001\u7b49\u65f6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u5b50\u7ec4\u4ef6\u7684 props \u91cd\u65b0\u6e32\u67d3\u65f6\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4f46\u5b9e\u9645\u4e0a\uff0c\u53ea\u6709\u6e32\u67d3 props \u65f6\u624d\u9700\u8981\u91cd\u65b0\u6e32\u67d3\u5b50\u7ec4\u4ef6\u3002\u5176\u4ed6\u4e00\u5207\u90fd\u662f\u4e0d\u5fc5\u8981\u7684\u3002<\/p>\n<p>\u30fbsrc\/example.js<\/p>\n<pre>mport react, { usestate } from \"react\";\nimport child from \".\/child\";\nimport \".\/example.css\";\n\nconst example = () =&gt; {\n  console.log(\"parent render\");\n  const [counta, setcounta] = usestate(0);\n  const [countb, setcountb] = usestate(0);\n  return (\n    &lt;div classname=\"parent\"&gt;\n      &lt;div&gt;\n        &lt;h3&gt;parent component&lt;\/h3&gt;\n        &lt;div&gt;\n          &lt;button\n            onclick={() =&gt; {\n              setcounta((pre) =&gt; pre + 1);\n            }}\n          &gt;\n            button a\n          &lt;\/button&gt;\n          &lt;span&gt;update the state of parent component&lt;\/span&gt;\n        &lt;\/div&gt;\n        &lt;div&gt;\n          &lt;button\n            onclick={() =&gt; {\n              setcountb((pre) =&gt; pre + 1);\n            }}\n          &gt;\n            buton b\n          &lt;\/button&gt;\n          &lt;span&gt;update the state of child component&lt;\/span&gt;\n        &lt;\/div&gt;\n      &lt;\/div&gt;\n      &lt;div&gt;\n        &lt;p&gt;the count of clicked\uff1a{counta}&lt;\/p&gt;\n      &lt;\/div&gt;\n      &lt;child countb={countb} \/&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default example;\n<\/pre>\n<p>\u30fbsrc\/child .js<\/p>\n<pre>const child = ({ countb }) =&gt; {\n  console.log(\"%cchild render\", \"color: red;\");\n\n  return (\n    &lt;div classname=\"child\"&gt;\n      &lt;h2&gt;child component&lt;\/h2&gt;\n      &lt;span&gt;the count of b button cliked\uff1a{countb}&lt;\/span&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default child;\n\n<\/pre>\n<ul>\n<li>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5f53\u6211\u4eec\u6309\u4e0b a\uff08\u7236\u7ec4\u4ef6\uff09\u6309\u94ae\u65f6\uff0c\u5b50\u7ec4\u4ef6\u5c31\u4f1a\u88ab\u6e32\u67d3\u3002\u867d\u7136\u6ca1\u6709\u5fc5\u8981\u3002<\/li>\n<\/ul>\n<p>\u30fb\u50cf\u8fd9\u6837\u3002<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173227090567405b39e5d95.jpg\" class=\"aligncenter\" title=\"React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u63d2\u56fe\" alt=\"React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u63d2\u56fe\" \/><\/p>\n<p>\u30fbsrc\/child .js\u200b\uff08\u4f7f\u7528\u5907\u5fd8\u5f55\u94a9\u5b50\uff09<\/p>\n<pre>import { memo } from \"react\";\n\nfunction areEqual(prevProps, nextProps) {\n  if (prevProps.countB !== nextProps.countB) {\n    return false; \/\/ re-rendered\n  } else {\n    return true; \/\/ not-re-rendred\n  }\n}\n\nconst ChildMemo = memo(({ countB }) =&gt; {\n  console.log(\"%cChild render\", \"color: red;\");\n\n  return (\n    &lt;div className=\"child\"&gt;\n      &lt;h2&gt;Child component&lt;\/h2&gt;\n      &lt;span&gt;The count of B button cliked\uff1a{countB}&lt;\/span&gt;\n    &lt;\/div&gt;\n  );\n}, areEqual);\n\nexport default ChildMemo;\n\n<\/pre>\n<ul>\n<li>\u5982\u679c\u6211\u4eec\u4f7f\u7528memo\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u3002<\/li>\n<\/ul>\n<p>\u30fb\u50cf\u8fd9\u6837\u3002<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173227090567405b39ea640.jpg\" class=\"aligncenter\" title=\"React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u63d2\u56fe1\" alt=\"React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u63d2\u56fe1\" \/><\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aReact \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u52aa\u6bd4\u4e9a\u624b\u673a\u5982\u4f55\u8fde\u63a5\u7535\u8111\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>React \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55 \u6536\u85cf \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u6587\u7ae0\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u300aReact \u57fa\u7840\u77e5\u8bc6~\u6e32\u67d3\u6027\u80fd\/\u5907\u5fd8\u5f55\u300b\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ \u8fd9\u4e9b\u662f\u5b50\u7ec4\u4ef6\u5c06\u88ab\u6e32\u67d3\u7684\u6a21\u5f0f\u3002 \u5f53\u7236\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3\u65f6\uff0c\u4f8b\u5982\u66f4\u65b0\u81ea\u8eab\u72b6\u6001\u7b49\u65f6\u3002 \u5f53\u5b50\u7ec4\u4ef6\u7684 props \u91cd\u65b0\u6e32\u67d3\u65f6\u3002 \u4f46\u5b9e\u9645\u4e0a\uff0c\u53ea\u6709\u6e32\u67d3 props \u65f6\u624d\u9700\u8981\u91cd\u65b0\u6e32\u67d3\u5b50\u7ec4\u4ef6\u3002\u5176\u4ed6\u4e00\u5207\u90fd\u662f\u4e0d\u5fc5\u8981\u7684\u3002 \u30fbsrc\/example.js mport react, { usestate } from &#8220;react&#8221;; import child from &#8220;.\/child&#8221;; import &#8220;.\/example.css&#8221;; const example = () =&gt; { console.log(&#8220;parent render&#8221;); const [counta, setcounta] = usestate(0); const [countb, setcountb] = usestate(0); return ( &lt;div classname=&#8221;parent&#8221;&gt; &lt;div&gt; &lt;h3&gt;parent component&lt;\/h3&gt; &lt;div&gt; &lt;button onclick={() [&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-52694","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52694","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=52694"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52694\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}