{"id":5869,"date":"2023-01-06T15:22:04","date_gmt":"2023-01-06T07:22:04","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5869"},"modified":"2023-01-06T15:22:04","modified_gmt":"2023-01-06T07:22:04","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8memoization%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8memoization%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Memoization\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, memoization\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 memoization\uff1f<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u61c9\u7528\u7a0b\u5e8f\u3002\u5176\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\u662f memoization\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u69cb\u5efa React \u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39 memoization \u5728 React \u4e2d\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f memoization\uff1f<\/h2>\n<p>Memoization \u662f\u4e00\u7a2e\u7de9\u5b58\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u69cb\u5efa\u61c9\u7528\u7a0b\u5e8f\u3002\u5b83\u7684\u539f\u7406\u662f\u5c07\u51fd\u6578\u7684\u8f38\u51fa\u503c\u7de9\u5b58\u8d77\u4f86\uff0c\u4ee5\u4fbf\u5728\u4e0b\u6b21\u8abf\u7528\u8a72\u51fd\u6578\u6642\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f9e\u7de9\u5b58\u4e2d\u7372\u53d6\u8f38\u51fa\u503c\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u8a08\u7b97\u3002\u9019\u6a23\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u8a08\u7b97\u3002<\/p>\n<h2>React \u4e2d\u7684 memoization<\/h2>\n<p>React \u4e2d\u7684 memoization \u53ef\u4ee5\u7528\u65bc\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u3002\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u69cb\u5efa\u61c9\u7528\u7a0b\u5e8f\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n<p>React \u4e2d\u7684 memoization \u53ef\u4ee5\u901a\u904e\u4f7f\u7528 React.memo() \u4f86\u5be6\u73fe\u3002React.memo() \u662f\u4e00\u500b\u9ad8\u968e\u7d44\u4ef6\uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u51fd\u6578\u7d44\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u51fd\u6578\u7d44\u4ef6\u3002\u65b0\u7684\u51fd\u6578\u7d44\u4ef6\u6703\u6bd4\u8f03\u50b3\u5165\u7684 props\uff0c\u5982\u679c props \u6c92\u6709\u6539\u8b8a\uff0c\u5247\u4e0d\u6703\u91cd\u65b0\u6e32\u67d3\u7d44\u4ef6\u3002<\/p>\n<pre class=\"brush: javascript\">\nconst MyComponent = React.memo(function MyComponent(props) {\n  \/* render using props *\/\n});\n<\/pre>\n<p>\u53e6\u5916\uff0cReact \u4e5f\u63d0\u4f9b\u4e86\u4e00\u500b useMemo Hook\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u7de9\u5b58\u51fd\u6578\u7684\u8f38\u51fa\u503c\u3002\u5b83\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff0c\u7b2c\u4e00\u500b\u53c3\u6578\u662f\u4e00\u500b\u51fd\u6578\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u4e00\u500b\u6578\u7d44\uff0c\u6578\u7d44\u4e2d\u7684\u503c\u6703\u7528\u65bc\u6bd4\u8f03\uff0c\u5982\u679c\u503c\u6c92\u6709\u6539\u8b8a\uff0c\u5247\u4e0d\u6703\u91cd\u65b0\u8abf\u7528\u51fd\u6578\u3002<\/p>\n<pre class=\"brush: javascript\">\nconst memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Memoization \u662f\u4e00\u7a2e\u7de9\u5b58\u6280\u8853\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u69cb\u5efa\u61c9\u7528\u7a0b\u5e8f\u3002React \u4e2d\u7684 memoization \u53ef\u4ee5\u901a\u904e\u4f7f\u7528 React.memo() \u548c useMemo Hook \u4f86\u5be6\u73fe\uff0c\u5b83\u5011\u53ef\u4ee5\u6e1b\u5c11\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39React\u4e2d\u4f7f\u7528Memoization\u7684\u65b9\u6cd5\uff0cMemoization\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u53ef\u4ee5\u63d0\u9ad8\u51fd\u6578\u7684\u6027\u80fd\uff0c\u4e26\u6e1b\u5c11\u91cd\u8907\u8a08\u7b97\u7684\u6642\u9593\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Memoization\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5869","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1wF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5869","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5869"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5869\/revisions"}],"predecessor-version":[{"id":5870,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5869\/revisions\/5870"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}