{"id":2401,"date":"2024-11-10T12:39:22","date_gmt":"2024-11-10T04:39:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2401\/"},"modified":"2024-11-10T12:39:22","modified_gmt":"2024-11-10T04:39:22","slug":"%e5%8f%8d%e5%ba%94%e8%ae%b0%e5%bf%86%e5%a4%87%e5%bf%98%e5%8d%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2401\/","title":{"rendered":"\u53cd\u5e94\u8bb0\u5fc6\u5907\u5fd8\u5355"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173072701569860.jpg\" class=\"aligncenter\" title=\"\u53cd\u5e94\u8bb0\u5fc6\u5907\u5fd8\u5355\u63d2\u56fe\" alt=\"\u53cd\u5e94\u8bb0\u5fc6\u5907\u5fd8\u5355\u63d2\u56fe\" \/><\/p>\n<p>react \u63d0\u4f9b\u4e86\u4e09\u79cd\u4e3b\u8981\u7684\u8bb0\u5fc6\u5de5\u5177\uff0c\u901a\u8fc7\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u548c\u91cd\u65b0\u8ba1\u7b97\u6765\u4f18\u5316\u7ec4\u4ef6\u6027\u80fd\uff1a<\/p>\n<h3> 1. <strong>usememo<\/strong> \u2013 \u8bb0\u5fc6\u8ba1\u7b97\u503c <\/h3>\n<ul>\n<li> <strong>\u7528\u9014<\/strong>\uff1a\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u4ec5\u5728\u4f9d\u8d56\u5173\u7cfb\u53d1\u751f\u53d8\u5316\u65f6\u91cd\u65b0\u8ba1\u7b97\u3002<\/li>\n<li>\n<p><strong>\u7528\u6cd5<\/strong>\uff1a\u7528\u4e8e<strong>\u6602\u8d35\u7684\u8ba1\u7b97<\/strong>\u6216<strong>\u6d3e\u751f\u6570\u636e<\/strong>\uff0c\u4ec5\u5e94\u4f7f\u7528\u7279\u5b9a\u4f9d\u8d56\u9879\u8fdb\u884c\u66f4\u65b0\u3002<\/p>\n<pre>const memoizedvalue = usememo(() =&amp;gt; complexcalculation(), [dependencies]);\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06\u51fd\u6570\u5185\u4f7f\u7528\u7684\u6240\u6709\u4f9d\u8d56\u9879\u5305\u542b\u5728\u4f9d\u8d56\u9879\u6570\u7ec4\u4e2d\u3002<\/li>\n<li>\u907f\u514d\u5728 usememo \u4e2d\u521b\u5efa\u65b0\u7684\u5f15\u7528\uff08\u6570\u7ec4\u3001\u5bf9\u8c61\uff09\u6216\u5185\u8054\u51fd\u6570\u3002<\/li>\n<li> <strong>\u6ce8\u610f<\/strong>\uff1a\u4e0d\u8981\u5c06 usememo \u7528\u4e8e\u51fd\u6570\uff1b\u5b83\u7f13\u5b58\u503c\uff0c\u800c\u4e0d\u662f\u51fd\u6570\u5f15\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3> 2. <strong>usecallback<\/strong> \u2013 memoize \u51fd\u6570\u53c2\u8003 <\/h3>\n<ul>\n<li> <strong>\u7528\u9014<\/strong>\uff1a\u7f13\u5b58\u51fd\u6570\u5f15\u7528\uff0c\u9632\u6b62\u5728\u6bcf\u6b21\u6e32\u67d3\u65f6\u91cd\u65b0\u521b\u5efa\u3002<\/li>\n<li>\n<p><strong>\u7528\u6cd5<\/strong>\uff1a\u7528\u4e8e<strong>\u7a33\u5b9a\u51fd\u6570\u5f15\u7528<\/strong>\uff0c\u7279\u522b\u662f\u7528\u4e8e\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u7684\u56de\u8c03\uff08\u4f8b\u5982\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff09\u3002<\/p>\n<pre>const memoizedfunction = usecallback(() =&amp;gt; { \/* logic *\/ }, [dependencies]);\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06\u51fd\u6570\u5185\u4f7f\u7528\u7684\u6240\u6709\u4f9d\u8d56\u9879\u5305\u542b\u5728\u4f9d\u8d56\u9879\u6570\u7ec4\u4e2d\uff0c\u4ee5\u907f\u514d\u8fc7\u65f6\u7684\u503c\u3002<\/li>\n<li>\u907f\u514d\u5728 usecallback \u4e2d\u58f0\u660e\u5185\u8054\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u7834\u574f\u8bb0\u5fc6\u3002<\/li>\n<li> <strong>\u6ce8\u610f<\/strong>\uff1a\u4ec5\u5bf9\u51fd\u6570\u4f7f\u7528 usecallback\u3002\u9519\u8bef\u5730\u4f7f\u7528 usecallback \u6765\u83b7\u53d6\u503c\u4f1a\u5bfc\u81f4\u4f4e\u6548\u7684\u4ee3\u7801\u548c\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3> 3. <strong>react.memo<\/strong> \u2013 \u8bb0\u5fc6\u6574\u4e2a\u7ec4\u4ef6 <\/h3>\n<ul>\n<li> <strong>\u7528\u9014<\/strong>\uff1a\u5982\u679c\u529f\u80fd\u7ec4\u4ef6\u7684<strong>\u9053\u5177\u672a\u66f4\u6539<\/strong>\uff0c\u5219\u9632\u6b62\u91cd\u65b0\u6e32\u67d3\u3002<\/li>\n<li>\n<p><strong>\u7528\u6cd5<\/strong>\uff1a\u7528\u4e8e<strong>\u4f18\u5316\u5b50\u7ec4\u4ef6<\/strong>\uff0c\u5f53\u7236\u7ec4\u4ef6\u53d1\u751f\u53d8\u5316\u65f6\u4e0d\u9700\u8981\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n<pre>const MemoizedComponent = React.memo(ChildComponent);\n\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e0e\u63a5\u6536\u7a33\u5b9a props \u6216\u5f88\u5c11\u66f4\u6539\u7684 props \u7684\u7ec4\u4ef6\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li>\u907f\u514d\u9891\u7e41\u66f4\u6539 props\uff08\u4f8b\u5982\u65b0\u5bf9\u8c61\/\u6570\u7ec4\uff09\uff0c\u4ee5\u6700\u5927\u9650\u5ea6\u5730\u63d0\u9ad8 react.memo \u7684\u6709\u6548\u6027\u3002<\/li>\n<li> <strong>\u6ce8\u610f<\/strong>\uff1a\u4e0e usecallbackmemoized \u51fd\u6570\u914d\u5408\u826f\u597d\uff0c\u53ef\u4ee5\u7ef4\u62a4\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u7684\u7a33\u5b9a props\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr>\n<h3> \u9700\u8981\u8bb0\u4f4f\u7684\u8981\u70b9 <\/h3>\n<ul>\n<li> <strong>\u4f7f\u7528 usememo \u4f5c\u4e3a\u503c<\/strong> \u548c <strong>usecallback \u4f5c\u4e3a\u51fd\u6570<\/strong>\u3002\n<ul>\n<li> <strong>\u5bf9\u51fd\u6570\u4f7f\u7528 usememo<\/strong> \u4f1a\u5bfc\u81f4\u7acb\u5373\u6267\u884c\uff0c\u800c\u4e0d\u662f\u7a33\u5b9a\u7684\u51fd\u6570\u5f15\u7528\u3002<\/li>\n<li> <strong>\u5bf9\u503c\u4f7f\u7528 usecallback<\/strong> \u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u4ee3\u7801\u6548\u7387\u4f4e\u4e0b\uff0c\u9700\u8981\u989d\u5916\u7684\u51fd\u6570\u8c03\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u8bb0\u5fc6\u603b\u7ed3<\/strong>\uff1a\n<ul>\n<li> <strong>usememo<\/strong>\uff1a\u7f13\u5b58<strong>\u8ba1\u7b97\u503c<\/strong>\uff08\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff09\u3002<\/li>\n<li> <strong>usecallback<\/strong>\uff1a\u7f13\u5b58<strong>\u51fd\u6570\u5f15\u7528<\/strong>\uff08\u56de\u8c03\uff09\u3002<\/li>\n<li> <strong>react.memo<\/strong>\uff1a\u57fa\u4e8e props \u7f13\u5b58 <strong>\u6574\u4e2a\u7ec4\u4ef6<\/strong>\uff0c\u4ee5\u9632\u6b62\u7236\u66f4\u65b0\u91cd\u65b0\u6e32\u67d3\u3002<\/li>\n<\/ul>\n<\/li>\n<li> <strong>\u6709\u9009\u62e9\u5730\u4f7f\u7528\u8bb0\u5fc6\u5316<\/strong>\uff1a\u6b63\u786e\u4f7f\u7528\u8bb0\u5fc6\u5316\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\uff0c\u4f46\u5982\u679c\u8fc7\u5ea6\u4f7f\u7528\u6216\u8bef\u7528\uff0c\u5219\u4f1a\u589e\u52a0\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u53cd\u5e94\u8bb0\u5fc6\u5907\u5fd8\u5355\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 \u63d0\u4f9b\u4e86\u4e09\u79cd\u4e3b\u8981\u7684\u8bb0\u5fc6\u5de5\u5177\uff0c\u901a\u8fc7\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u548c\u91cd\u65b0\u8ba1\u7b97\u6765\u4f18\u5316\u7ec4\u4ef6\u6027\u80fd\uff1a 1. usememo \u2013 \u8bb0\u5fc6\u8ba1\u7b97\u503c \u7528\u9014\uff1a\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u4ec5\u5728\u4f9d\u8d56\u5173\u7cfb\u53d1\u751f\u53d8\u5316\u65f6\u91cd\u65b0\u8ba1\u7b97\u3002 \u7528\u6cd5\uff1a\u7528\u4e8e\u6602\u8d35\u7684\u8ba1\u7b97\u6216\u6d3e\u751f\u6570\u636e\uff0c\u4ec5\u5e94\u4f7f\u7528\u7279\u5b9a\u4f9d\u8d56\u9879\u8fdb\u884c\u66f4\u65b0\u3002 const memoizedvalue = usememo(() =&amp;gt; complexcalculation(), [dependencies]); \u767b\u5f55\u540e\u590d\u5236 \u6700\u4f73\u5b9e\u8df5\uff1a \u5c06\u51fd\u6570\u5185\u4f7f\u7528\u7684\u6240\u6709\u4f9d\u8d56\u9879\u5305\u542b\u5728\u4f9d\u8d56\u9879\u6570\u7ec4\u4e2d\u3002 \u907f\u514d\u5728 usememo \u4e2d\u521b\u5efa\u65b0\u7684\u5f15\u7528\uff08\u6570\u7ec4\u3001\u5bf9\u8c61\uff09\u6216\u5185\u8054\u51fd\u6570\u3002 \u6ce8\u610f\uff1a\u4e0d\u8981\u5c06 usememo \u7528\u4e8e\u51fd\u6570\uff1b\u5b83\u7f13\u5b58\u503c\uff0c\u800c\u4e0d\u662f\u51fd\u6570\u5f15\u7528\u3002 2. usecallback \u2013 memoize \u51fd\u6570\u53c2\u8003 \u7528\u9014\uff1a\u7f13\u5b58\u51fd\u6570\u5f15\u7528\uff0c\u9632\u6b62\u5728\u6bcf\u6b21\u6e32\u67d3\u65f6\u91cd\u65b0\u521b\u5efa\u3002 \u7528\u6cd5\uff1a\u7528\u4e8e\u7a33\u5b9a\u51fd\u6570\u5f15\u7528\uff0c\u7279\u522b\u662f\u7528\u4e8e\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\u7684\u56de\u8c03\uff08\u4f8b\u5982\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff09\u3002 const memoizedfunction = usecallback(() =&amp;gt; { \/* logic *\/ }, [dependencies]); \u767b\u5f55\u540e\u590d\u5236 \u6700\u4f73\u5b9e\u8df5\uff1a \u5c06\u51fd\u6570\u5185\u4f7f\u7528\u7684\u6240\u6709\u4f9d\u8d56\u9879\u5305\u542b\u5728\u4f9d\u8d56\u9879\u6570\u7ec4\u4e2d\uff0c\u4ee5\u907f\u514d\u8fc7\u65f6\u7684\u503c\u3002 \u907f\u514d\u5728 usecallback \u4e2d\u58f0\u660e\u5185\u8054\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u7834\u574f\u8bb0\u5fc6\u3002 \u6ce8\u610f\uff1a\u4ec5\u5bf9\u51fd\u6570\u4f7f\u7528 usecallback\u3002\u9519\u8bef\u5730\u4f7f\u7528 usecallback \u6765\u83b7\u53d6\u503c\u4f1a\u5bfc\u81f4\u4f4e\u6548\u7684\u4ee3\u7801\u548c\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u8c03\u7528\u3002 3. react.memo \u2013 [&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-2401","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2401","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=2401"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2401\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}