{"id":53680,"date":"2024-12-03T08:13:41","date_gmt":"2024-12-03T00:13:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53680\/"},"modified":"2024-12-03T08:13:41","modified_gmt":"2024-12-03T00:13:41","slug":"usememo-%e4%b8%8e-usecallback-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53680\/","title":{"rendered":"useMemo \u4e0e useCallback"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>useMemo \u4e0e useCallback<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300auseMemo \u4e0e useCallback\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h2> <strong>\u7b80\u4ecb<\/strong> <\/h2>\n<p>react \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u94a9\u5b50\u6765\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u6784\u5efa\u52a8\u6001\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e9b\u94a9\u5b50\u4e2d\uff0c<strong>usememo<\/strong>\u548c<strong>usecallback<\/strong>\u662f\u63d0\u9ad8\u7ec4\u4ef6\u6027\u80fd\u7684\u91cd\u8981\u5de5\u5177\u3002\u5c3d\u7ba1\u4e24\u8005\u90fd\u6709\u76f8\u4f3c\u7684\u76ee\u7684\u2014\u2014\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u8ba1\u7b97\u6216\u51fd\u6570\u91cd\u65b0\u521b\u5efa\u2014\u2014\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 usememo \u548c usecallback \u4e4b\u95f4\u7684\u533a\u522b\u3001\u5b83\u4eec\u4e3a\u4f55\u6709\u7528\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u6709\u6548\u5730\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<hr>\n<h2> <strong>1. usememo \u662f\u4ec0\u4e48\uff1f<\/strong> <\/h2>\n<p><strong>usememo<\/strong> \u94a9\u5b50\u7528\u4e8e<strong>\u8bb0\u5fc6\u6602\u8d35\u8ba1\u7b97\u7684\u7ed3\u679c<\/strong>\u5e76\u4e14\u4ec5\u5728\u5176\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\u65f6\u624d\u91cd\u65b0\u8ba1\u7b97\u5b83\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u60a8\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u8ba1\u7b97\u503c\uff0c\u8fd9\u5bf9\u4e8e\u8ba1\u7b97\u6210\u672c\u8f83\u9ad8\u7684\u64cd\u4f5c\u7279\u522b\u6709\u7528\u3002<\/p>\n<ul>\n<li> <strong>\u8bed\u6cd5<\/strong> <\/li>\n<\/ul>\n<pre>const memoizedvalue = usememo(() =&gt; computeexpensivevalue(a, b), [a, b]);\n<\/pre>\n<ul>\n<li> <strong>\u7528\u6cd5\u793a\u4f8b<\/strong> <\/li>\n<\/ul>\n<pre>import react, { usememo } from 'react';\n\nfunction example({ items }) {\n  const total = usememo(() =&gt; {\n    return items.reduce((acc, item) =&gt; acc + item.price, 0);\n  }, [items]);\n\n  return &lt;div&gt;total price: {total}&lt;\/div&gt;;\n}\n<\/pre>\n<p>\u8fd9\u91cc\uff0c<strong>usememo<\/strong>\u53ea\u4f1a\u5728\u9879\u76ee\u53d1\u751f\u53d8\u5316\u65f6\u91cd\u65b0\u8ba1\u7b97\u603b\u8ba1\uff0c\u5982\u679c\u9879\u76ee\u662f\u9759\u6001\u7684\u6216\u5f88\u5c11\u66f4\u65b0\uff0c\u5219\u53ef\u4ee5\u8282\u7701\u8d44\u6e90\u3002<\/p>\n<hr>\n<h2> <strong>1.\u4ec0\u4e48\u662f usecallback\uff1f<\/strong> <\/h2>\n<p><strong>usecallback<\/strong> \u94a9\u5b50\u7528\u4e8e<strong>\u8bb0\u5fc6\u51fd\u6570<\/strong>\u3002\u4e0e usememo \u4e00\u6837\uff0c\u5b83\u4ec5\u5728\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\u65f6\u91cd\u65b0\u8ba1\u7b97\u51fd\u6570\u3002 usecallback \u5bf9\u4e8e\u9632\u6b62\u5728\u6bcf\u6b21\u6e32\u67d3\u65f6\u91cd\u65b0\u521b\u5efa\u51fd\u6570\u7279\u522b\u6709\u7528\uff0c\u8fd9\u5728\u5c06\u56de\u8c03\u4f20\u9012\u7ed9\u4f9d\u8d56\u4e8e\u5f15\u7528\u76f8\u7b49\u6027\u7684\u4f18\u5316\u5b50\u7ec4\u4ef6\u65f6\u53ef\u4ee5\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<ul>\n<li> <strong>\u8bed\u6cd5<\/strong> <\/li>\n<\/ul>\n<pre>const memoizedcallback = usecallback(() =&gt; {\n  dosomething(a, b);\n}, [a, b]);\n<\/pre>\n<ul>\n<li> <strong>\u7528\u6cd5\u793a\u4f8b<\/strong> <\/li>\n<\/ul>\n<pre>import React, { useCallback } from 'react';\n\nfunction ParentComponent() {\n  const handleClick = useCallback(() =&gt; {\n    console.log('Button clicked!');\n  }, []);\n\n  return &lt;ChildComponent onClick={handleClick} \/&gt;;\n}\n<\/pre>\n<p>\u8fd9\u91cc\uff0c<strong>usecallback<\/strong> \u786e\u4fddhandleclick \u4fdd\u6301\u76f8\u540c\u7684\u51fd\u6570\u5b9e\u4f8b\uff0c\u9664\u975e\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\uff0c\u4ece\u800c\u6709\u52a9\u4e8e\u9632\u6b62 childcomponent \u4e2d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n<hr>\n<h2> <strong>3.\u6bd4\u8f83 usememo \u548c usecallback<\/strong> <\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241203\/1733215554674ec542d0bd2.jpg\" class=\"aligncenter\" title=\"useMemo \u4e0e useCallback\u63d2\u56fe\" alt=\"useMemo \u4e0e useCallback\u63d2\u56fe\" \/><\/p>\n<p><strong>\u8981\u70b9<\/strong><\/p>\n<ul>\n<li> \u5f53\u60a8\u60f3\u8981<strong>\u7f13\u5b58<\/strong>\u8ba1\u7b97\u7684<strong>\u7ed3\u679c<\/strong>\u65f6\uff0c<strong>usememo<\/strong>\u5f88\u6709\u7528\u3002<\/li>\n<li> \u5f53\u60a8\u60f3\u8981<strong>\u7f13\u5b58<\/strong>\u4e00\u4e2a<strong>\u51fd\u6570<\/strong>\u4ee5\u907f\u514d\u91cd\u65b0\u521b\u5efa\u5b83\u65f6\uff0c<strong>usecallback<\/strong>\u975e\u5e38\u6709\u7528\u3002<\/li>\n<\/ul>\n<hr>\n<h2> <strong>4.\u4f55\u65f6\u4f7f\u7528\u6bcf\u4e2a\u94a9\u5b50\uff1f<\/strong> <\/h2>\n<p>\u77e5\u9053\u4f55\u65f6\u4f7f\u7528 <strong>usememo<\/strong> \u548c <strong>usecallback<\/strong> \u5f52\u6839\u7ed3\u5e95\u662f\u4e86\u89e3\u7ec4\u4ef6\u7684\u6027\u80fd\u8981\u6c42\u4ee5\u53ca\u8bb0\u5fc6\u662f\u5426\u4f1a\u4ea7\u751f\u660e\u663e\u7684\u5dee\u5f02\u3002<\/p>\n<p><strong>\u4f7f\u7528 usememo:<\/strong><\/p>\n<ul>\n<li>\u5f53\u60a8\u8fdb\u884c\u8ba1\u7b97\u91cf\u5927\u4e14\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u6e32\u67d3\u65f6\u91cd\u65b0\u8fd0\u884c\u65f6\u3002<\/li>\n<li>\u8bb0\u5fc6\u6d3e\u751f\u6570\u636e\u6709\u52a9\u4e8e\u51cf\u5c11\u8ba1\u7b97\uff0c\u4f8b\u5982\u805a\u5408\u5927\u578b\u5217\u8868\u4e2d\u7684\u6570\u636e\u6216\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 usecallback:<\/strong><\/p>\n<ul>\n<li>\u5f53\u60a8\u5c06\u51fd\u6570\u4f5c\u4e3a prop \u4f20\u9012\u7ed9\u4f9d\u8d56\u4e8e\u5f15\u7528\u76f8\u7b49\u6027\u7684\u5b50\u7ec4\u4ef6\u65f6\u3002<\/li>\n<li>\u9632\u6b62\u4e0d\u5fc5\u8981\u5730\u91cd\u65b0\u521b\u5efa\u51fd\u6570\uff0c\u7279\u522b\u662f\u5728\u9ad8\u9891\u7ec4\u4ef6\u4e2d\u3002<\/li>\n<\/ul>\n<hr>\n<h2> <strong>5.\u5e38\u89c1\u9519\u8bef\u548c\u6700\u4f73\u5b9e\u8df5<\/strong> <\/h2>\n<ul>\n<li><strong>\u8fc7\u65e9\u4f7f\u7528 usememo \u6216 usecallback<\/strong><\/li>\n<\/ul>\n<p>\u4e0d\u8981\u8fc7\u5ea6\u4f7f\u7528\u8fd9\u4e9b\u94a9\u5b50\u3002\u8bb0\u5fc6\u5316\u4f1a\u589e\u52a0\u590d\u6742\u6027\uff0c\u5982\u679c\u4e0d\u9700\u8981\uff0c\u5b83\u4f1a\u589e\u52a0\u5185\u5b58\u5f00\u9500\uff0c\u4ece\u800c\u964d\u4f4e\u6027\u80fd\u3002<\/p>\n<ul>\n<li><strong>\u5ffd\u7565\u4f9d\u8d56\u5173\u7cfb<\/strong><\/li>\n<\/ul>\n<p>\u786e\u4fdd\u6b63\u786e\u5217\u51fa\u6240\u6709\u4f9d\u8d56\u9879\u3002\u5982\u679c\u4f9d\u8d56\u9879\u53d1\u751f\u66f4\u6539\u4f46\u672a\u5305\u542b\u5728\u6570\u7ec4\u4e2d\uff0c\u5219\u7f13\u5b58\u7684\u7ed3\u679c\u53ef\u80fd\u4f1a\u8fc7\u65f6\uff0c\u4ece\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<ul>\n<li><strong>\u6ee5\u7528 usememo \u548c usecallback<\/strong><\/li>\n<\/ul>\n<p>\u8bb0\u4f4f\uff1ausememo \u7f13\u5b58\u503c\uff0cusecallback \u7f13\u5b58\u51fd\u6570\u3002\u4f7f\u7528\u9519\u8bef\u7684\u94a9\u5b50\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a\u548c\u9519\u8bef\u3002<\/p>\n<ul>\n<li><strong>\u907f\u514d\u91cd\u65b0\u8bb0\u5fc6\u9677\u9631<\/strong><\/li>\n<\/ul>\n<p>\u8bb0\u5fc6\u7684\u51fd\u6570\u548c\u503c\u4ec5\u5728\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\u65f6\u624d\u4f1a\u66f4\u65b0\u3002\u4e3a\u4e86\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u6e32\u67d3\uff0c\u8bf7\u786e\u4fdd\u4f9d\u8d56\u9879\u6570\u7ec4\u4ec5\u5305\u542b\u771f\u6b63\u5f71\u54cd\u51fd\u6570\u7ed3\u679c\u6216\u903b\u8f91\u7684\u53d8\u91cf\u3002<\/p>\n<hr>\n<h2> <strong>\u7ed3\u8bba<\/strong> <\/h2>\n<p><strong>usememo<\/strong> \u548c <strong>usecallback<\/strong> \u90fd\u662f\u4f18\u5316 react \u5e94\u7528\u7a0b\u5e8f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u901a\u8fc7<strong>\u7f13\u5b58<\/strong><strong>\u8ba1\u7b97<\/strong>\u4e0e<strong>usememo<\/strong>\u548c<strong>\u51fd\u6570<\/strong>\u4e0e<strong>usecallback<\/strong>\uff0c\u60a8\u53ef\u4ee5<strong>\u63d0\u9ad8\u6027\u80fd<\/strong> \uff0c\u5c24\u5176\u662f\u5728\u8ba1\u7b97\u91cf\u5927\u6216\u7ec4\u4ef6\u6e32\u67d3\u9891\u7e41\u7684\u5e94\u7528\u4e2d\u3002<\/p>\n<p>\u867d\u7136\u8fd9\u4e9b\u94a9\u5b50\u5f88\u6709\u7528\uff0c\u4f46\u660e\u667a\u5730\u4f7f\u7528\u5b83\u4eec\u4e5f\u5f88\u91cd\u8981\u3002\u901a\u8fc7\u7b56\u7565\u6027\u5730\u5e94\u7528 usememo \u548c usecallback\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684 react \u5e94\u7528\u7a0b\u5e8f\u4fdd\u6301\u5feb\u901f\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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   \u7535\u8111\u624b\u5199\u8f93\u5165\u6cd5\uff1a\u5feb\u901f\u5b9e\u73b0\u7535\u5b50\u6587\u5b57\u8f93\u5165\u7684\u5229\u5668\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>useMemo \u4e0e useCallback \u6536\u85cf \u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u7c73\u4e91\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300auseMemo \u4e0e useCallback\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b \u7b80\u4ecb react \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u94a9\u5b50\u6765\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u6784\u5efa\u52a8\u6001\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e9b\u94a9\u5b50\u4e2d\uff0cusememo\u548cusecallback\u662f\u63d0\u9ad8\u7ec4\u4ef6\u6027\u80fd\u7684\u91cd\u8981\u5de5\u5177\u3002\u5c3d\u7ba1\u4e24\u8005\u90fd\u6709\u76f8\u4f3c\u7684\u76ee\u7684\u2014\u2014\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u8ba1\u7b97\u6216\u51fd\u6570\u91cd\u65b0\u521b\u5efa\u2014\u2014\u4f46\u5b83\u4eec\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8 usememo \u548c usecallback \u4e4b\u95f4\u7684\u533a\u522b\u3001\u5b83\u4eec\u4e3a\u4f55\u6709\u7528\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u9879\u76ee\u4e2d\u6709\u6548\u5730\u4f7f\u7528\u5b83\u4eec\u3002 1. usememo \u662f\u4ec0\u4e48\uff1f usememo \u94a9\u5b50\u7528\u4e8e\u8bb0\u5fc6\u6602\u8d35\u8ba1\u7b97\u7684\u7ed3\u679c\u5e76\u4e14\u4ec5\u5728\u5176\u4f9d\u8d56\u9879\u53d1\u751f\u53d8\u5316\u65f6\u624d\u91cd\u65b0\u8ba1\u7b97\u5b83\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u60a8\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u91cd\u65b0\u8ba1\u7b97\u503c\uff0c\u8fd9\u5bf9\u4e8e\u8ba1\u7b97\u6210\u672c\u8f83\u9ad8\u7684\u64cd\u4f5c\u7279\u522b\u6709\u7528\u3002 \u8bed\u6cd5 const memoizedvalue = usememo(() =&gt; computeexpensivevalue(a, b), [a, b]); \u7528\u6cd5\u793a\u4f8b import react, { usememo } from &#8216;react&#8217;; function example({ items }) { const total = usememo(() =&gt; { return items.reduce((acc, item) =&gt; acc + [&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-53680","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53680","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=53680"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53680\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}