{"id":5646,"date":"2022-05-12T11:17:31","date_gmt":"2022-05-12T03:17:31","guid":{"rendered":"http:\/\/39.106.39.94\/?p=5646"},"modified":"2022-05-12T11:17:31","modified_gmt":"2022-05-12T03:17:31","slug":"effect","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=5646","title":{"rendered":"Effect"},"content":{"rendered":"\n<p>React\u7ec4\u4ef6\u6709\u90e8\u5206\u903b\u8f91\u90fd\u53ef\u4ee5\u76f4\u63a5\u7f16\u5199\u5230\u7ec4\u4ef6\u7684\u51fd\u6570\u4f53\u4e2d\u7684\uff0c\u50cf\u662f\u5bf9\u6570\u7ec4\u8c03\u7528filter\u3001map\u7b49\u65b9\u6cd5\uff0c\u50cf\u662f\u5224\u65ad\u67d0\u4e2a\u7ec4\u4ef6\u662f\u5426\u663e\u793a\u7b49\u3002\u4f46\u662f\u6709\u4e00\u90e8\u5206\u903b\u8f91\u5982\u679c\u76f4\u63a5\u5199\u5728\u51fd\u6570\u4f53\u4e2d\uff0c\u4f1a\u5f71\u54cd\u5230\u7ec4\u4ef6\u7684\u6e32\u67d3\uff0c\u8fd9\u90e8\u5206\u4f1a\u4ea7\u751f\u201c\u526f\u4f5c\u7528\u201d\u7684\u4ee3\u7801\uff0c\u662f\u4e00\u5b9a\u4e0d\u80fd\u76f4\u63a5\u5199\u5728\u51fd\u6570\u4f53\u4e2d\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u76f4\u63a5\u5c06\u4fee\u6539state\u7684\u903b\u8f91\u7f16\u5199\u5230\u4e86\u7ec4\u4ef6\u4e4b\u4e2d\uff0c\u5c31\u4f1a\u5bfc\u81f4\u7ec4\u4ef6\u4e0d\u65ad\u7684\u5faa\u73af\u6e32\u67d3\uff0c\u76f4\u81f3\u8c03\u7528\u6b21\u6570\u8fc7\u591a\u5185\u5b58\u6ea2\u51fa\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">React.StrictMode<\/h2>\n\n\n\n<p>\u7f16\u5199React\u7ec4\u4ef6\u65f6\uff0c\u6211\u4eec\u8981\u6781\u529b\u7684\u907f\u514d\u7ec4\u4ef6\u4e2d\u51fa\u73b0\u90a3\u4e9b\u4f1a\u4ea7\u751f\u201c\u526f\u4f5c\u7528\u201d\u7684\u4ee3\u7801\u3002\u540c\u65f6\uff0c\u5982\u679c\u4f60\u7684React\u4f7f\u7528\u4e86\u4e25\u683c\u6a21\u5f0f\uff0c\u4e5f\u5c31\u662f\u5728React\u4e2d\u4f7f\u7528\u4e86<code>React.StrictMode<\/code>\u6807\u7b7e\uff0c\u90a3\u4e48React\u4f1a\u975e\u5e38\u201c\u667a\u80fd\u201d\u7684\u53bb\u68c0\u67e5\u4f60\u7684\u7ec4\u4ef6\u4e2d\u662f\u5426\u5199\u6709\u526f\u4f5c\u7528\u7684\u4ee3\u7801\uff0c\u5f53\u7136\u8fd9\u4e2a\u667a\u80fd\u662f\u52a0\u4e86\u5f15\u53f7\u7684\uff0c\u6211\u4eec\u6765\u770b\u770bReact\u5b98\u7f51\u7684\u6587\u6863\u662f\u5982\u4f55\u8bf4\u660e\u7684\uff1a<\/p>\n\n\n\n<p>Strict mode can\u2019t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:<\/p>\n\n\n\n<ul><li>Class component&nbsp;<code>constructor<\/code>,&nbsp;<code>render<\/code>, and&nbsp;<code>shouldComponentUpdate<\/code>&nbsp;methods<\/li><li>Class component static&nbsp;<code>getDerivedStateFromProps<\/code>&nbsp;method<\/li><li>Function component bodies<\/li><li>State updater functions (the first argument to&nbsp;<code>setState<\/code>)<\/li><li>Functions passed to&nbsp;<code>useState<\/code>,&nbsp;<code>useMemo<\/code>, or&nbsp;<code>useReducer<\/code><\/li><\/ul>\n\n\n\n<p>\u4e0a\u6587\u7684\u5173\u952e\u5b57\u53eb\u505a\u201cdouble-invoking\u201d\u5373\u91cd\u590d\u8c03\u7528\uff0c\u8fd9\u53e5\u8bdd\u662f\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u5927\u6982\u610f\u601d\u5c31\u662f\uff0cReact\u5e76\u4e0d\u80fd\u81ea\u52a8\u66ff\u4f60\u53d1\u73b0\u526f\u4f5c\u7528\uff0c\u4f46\u662f\u5b83\u4f1a\u60f3\u529e\u6cd5\u8ba9\u5b83\u663e\u73b0\u51fa\u6765\uff0c\u4ece\u800c\u8ba9\u4f60\u53d1\u73b0\u5b83\u3002\u90a3\u4e48\u5b83\u662f\u600e\u4e48\u8ba9\u4f60\u53d1\u73b0\u526f\u4f5c\u7528\u7684\u5462\uff1fReact\u7684\u4e25\u683c\u6a21\u5f0f\uff0c\u5728\u5904\u4e8e\u5f00\u53d1\u6a21\u5f0f\u4e0b\uff0c\u4f1a\u4e3b\u52a8\u7684\u91cd\u590d\u8c03\u7528\u4e00\u4e9b\u51fd\u6570\uff0c\u4ee5\u4f7f\u526f\u4f5c\u7528\u663e\u73b0\u3002\u6240\u4ee5\u5728\u5904\u4e8e\u5f00\u53d1\u6a21\u5f0f\u4e14\u5f00\u542f\u4e86React\u4e25\u683c\u6a21\u5f0f\u65f6\uff0c\u8fd9\u4e9b\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u4e24\u6b21\uff1a<\/p>\n\n\n\n<p>\u7c7b\u7ec4\u4ef6\u7684\u7684&nbsp;<code>constructor<\/code>,&nbsp;<code>render<\/code>, \u548c&nbsp;<code>shouldComponentUpdate<\/code>&nbsp;\u65b9\u6cd5<br>\u7c7b\u7ec4\u4ef6\u7684\u9759\u6001\u65b9\u6cd5&nbsp;<code>getDerivedStateFromProps<\/code><br>\u51fd\u6570\u7ec4\u4ef6\u7684\u51fd\u6570\u4f53<br>\u53c2\u6570\u4e3a\u51fd\u6570\u7684<code>setState<\/code><br>\u53c2\u6570\u4e3a\u51fd\u6570\u7684<code>useState<\/code>,&nbsp;<code>useMemo<\/code>, or&nbsp;<code>useReducer<\/code><\/p>\n\n\n\n<p>\u91cd\u590d\u7684\u8c03\u7528\u4f1a\u4f7f\u526f\u4f5c\u7528\u66f4\u5bb9\u6613\u51f8\u663e\u51fa\u6765\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u7740\u5728\u51fd\u6570\u7ec4\u4ef6\u7684\u51fd\u6570\u4f53\u4e2d\u8c03\u7528\u4e00\u4e2a<code>console.log<\/code>\u4f60\u4f1a\u53d1\u73b0\u5b83\u4f1a\u6267\u884c\u4e24\u6b21\uff0c\u5982\u679c\u4f60\u7684\u6d4f\u89c8\u5668\u4e2d\u5b89\u88c5\u4e86React Developer Tools\uff0c\u7b2c\u4e8c\u6b21\u8c03\u7528\u4f1a\u663e\u793a\u4e3a\u7070\u8272\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u65e0\u6cd5\u901a\u8fc7\u6d4f\u89c8\u5668\u6b63\u5e38\u5b89\u88c5<a href=\"https:\/\/my-wp.oss-cn-beijing.aliyuncs.com\/wp-content\/uploads\/2022\/05\/20220512111133423.zip\">React Developer Tools<\/a>\u53ef\u4ee5\u901a\u8fc7\u70b9\u51fb\u8fd9\u91cc\u4e0b\u8f7d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528Effect<a href=\"https:\/\/www.lilichao.com\/wp-admin\/edit.php?post_type=post\"><\/a><\/h2>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898React\u4e13\u95e8\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u94a9\u5b50\u51fd\u6570<code>useEffect()<\/code>\uff0cEffect\u7684\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u526f\u4f5c\u7528\uff0c\u4e13\u95e8\u7528\u6765\u5904\u7406\u90a3\u4e9b\u4e0d\u80fd\u76f4\u63a5\u5199\u5728\u7ec4\u4ef6\u5185\u90e8\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u54ea\u4e9b\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u5199\u5728\u7ec4\u4ef6\u5185\u90e8\u5462\uff1f\u50cf\u662f\uff1a\u83b7\u53d6\u6570\u636e\u3001\u8bb0\u5f55\u65e5\u5fd7\u3001\u68c0\u67e5\u767b\u5f55\u3001\u8bbe\u7f6e\u5b9a\u65f6\u5668\u7b49\u3002\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u90a3\u4e9b\u548c\u7ec4\u4ef6\u6e32\u67d3\u65e0\u5173\uff0c\u4f46\u5374\u6709\u53ef\u80fd\u5bf9\u7ec4\u4ef6\u4ea7\u751f\u526f\u4f5c\u7528\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>useEffect\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>useEffect(didUpdate);<\/code><\/pre>\n\n\n\n<p><code>useEffect()<\/code>\u9700\u8981\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">useEffect(()=&gt;{<br>    \/* \u7f16\u5199\u90a3\u4e9b\u4f1a\u4ea7\u751f\u526f\u4f5c\u7528\u7684\u4ee3\u7801 *\/<br>});<\/pre>\n\n\n\n<p><code>useEffect()<\/code>\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u4f1a\u5728\u7ec4\u4ef6\u6bcf\u6b21\u6e32\u67d3\u5b8c\u6bd5\u4e4b\u540e\u6267\u884c\uff0c\u8fd9\u4e5f\u662f\u5b83\u548c\u5199\u5728\u51fd\u6570\u4f53\u4e2d\u4ee3\u7801\u7684\u6700\u5927\u7684\u4e0d\u540c\uff0c\u51fd\u6570\u4f53\u4e2d\u7684\u4ee3\u7801\u4f1a\u5728\u7ec4\u4ef6\u6e32\u67d3\u524d\u6267\u884c\uff0c\u800c<code>useEffect()<\/code>\u4e2d\u7684\u4ee3\u7801\u662f\u5728\u7ec4\u4ef6\u6e32\u67d3\u540e\u624d\u6267\u884c\uff0c\u8fd9\u5c31\u907f\u514d\u4e86\u4ee3\u7801\u7684\u6267\u884c\u5f71\u54cd\u5230\u7ec4\u4ef6\u6e32\u67d3\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e2aHook\uff0c\u6211\u8bbe\u7f6e\u4e86React\u7ec4\u4ef6\u5728\u6e32\u67d3\u540e\u6240\u8981\u6267\u884c\u7684\u64cd\u4f5c\u3002React\u4f1a\u5c06\u6211\u4eec\u4f20\u9012\u7684\u51fd\u6570\u4fdd\u5b58\uff08\u6211\u4eec\u79f0\u8fd9\u4e2a\u51fd\u6570\u4e3aeffect\uff09\uff0c\u5e76\u4e14\u5728DOM\u66f4\u65b0\u540e\u6267\u884c\u8c03\u7528\u5b83\u3002React\u4f1a\u786e\u4fddeffect\u6bcf\u6b21\u8fd0\u884c\u65f6\uff0cDOM\u90fd\u5df2\u7ecf\u66f4\u65b0\u5b8c\u6bd5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6e05\u9664effect<\/h2>\n\n\n\n<p>\u7ec4\u4ef6\u7684\u6bcf\u6b21\u91cd\u65b0\u6e32\u67d3effect\u90fd\u4f1a\u6267\u884c\uff0c\u6709\u4e00\u4e9b\u60c5\u51b5\u91cc\uff0c\u4e24\u6b21effect\u6267\u884c\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u6bd4\u5982\uff0c\u5728effect\u4e2d\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u603b\u4e0d\u80fd\u6bcf\u6b21effect\u6267\u884c\u90fd\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u5b9a\u65f6\u5668\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5728\u4e00\u4e2aeffect\u6267\u884c\u524d\uff0c\u6e05\u9664\u6389\u524d\u4e00\u4e2aeffect\u6240\u5e26\u6765\u7684\u5f71\u54cd\u3002\u8981\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u53ef\u4ee5\u5728effect\u4e2d\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">useEffect(()=&gt;{<br>    \/* \u7f16\u5199\u90a3\u4e9b\u4f1a\u4ea7\u751f\u526f\u4f5c\u7528\u7684\u4ee3\u7801 *\/<br>    <br>    return () =&gt; {<br>        \/* \u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u4e0b\u4e00\u6b21effect\u6267\u884c\u94b1\u8c03\u7528 *\/<br>    };<br>});<\/pre>\n\n\n\n<p>effect\u8fd4\u56de\u7684\u51fd\u6570\uff0c\u4f1a\u5728\u4e0b\u4e00\u6b21effect\u6267\u884c\u524d\u8c03\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\u6e05\u9664\u6389\u524d\u4e00\u6b21effect\u6267\u884c\u6240\u5e26\u6765\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9650\u5236effect<\/h2>\n\n\n\n<p>\u7ec4\u4ef6\u6bcf\u6b21\u6e32\u67d3effect\u90fd\u4f1a\u6267\u884c\uff0c\u8fd9\u4f3c\u4e4e\u5e76\u4e0d\u603b\u90a3\u4e48\u5fc5\u8981\u3002\u56e0\u6b64\u5728<code>useEffect()<\/code>\u4e2d\u6211\u4eec\u53ef\u4ee5\u9650\u5236effect\u7684\u6267\u884c\u65f6\u673a\uff0c\u5728<code>useEffect()<\/code>\u4e2d\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f20\u9012\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">useEffect(()=&gt;{\n    \/* \u7f16\u5199\u90a3\u4e9b\u4f1a\u4ea7\u751f\u526f\u4f5c\u7528\u7684\u4ee3\u7801 *\/\n\n    return () =&gt; {\n        \/* \u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u4e0b\u4e00\u6b21effect\u6267\u884c\u524d\u8c03\u7528 *\/\n    };\n}, [a, b]);<\/pre>\n\n\n\n<p>\u4e0a\u4f8b\u4e2d\uff0c\u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a\u53d8\u91cfa\u548cb\uff0c\u8bbe\u7f6e\u4ee5\u540eeffect\u53ea\u6709\u5728\u53d8\u91cfa\u6216b\u53d1\u751f\u53d8\u5316\u65f6\u624d\u4f1a\u6267\u884c\u3002\u8fd9\u6837\u5373\u53ef\u9650\u5236effect\u7684\u6267\u884c\u6b21\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f20\u9012\u4e00\u4e2a\u7a7a\u6570\u7ec4\uff0c\u5982\u679c\u662f\u7a7a\u6570\u7ec4\uff0c\u90a3\u4e48effect\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f7f\u7528Effect\u4fee\u6539\u7ec3\u4e60<\/h2>\n\n\n\n<p>\u5728\u300a\u6c49\u5821\u5230\u5bb6\u300b\u7684\u7ec3\u4e60\u4e2d\uff0c\u5b58\u5728\u7740\u4e00\u4e2abug\u3002\u5f53\u6211\u4eec\u5728\u8d2d\u7269\u8f66\u6216\u7ed3\u8d26\u754c\u9762\u51cf\u5c11\u5546\u54c1\u7684\u6570\u91cf\u5168\u90e8\u4e3a0\u65f6\uff08\u8d2d\u7269\u8f66\u4e2d\u6ca1\u6709\u5546\u54c1\u65f6\uff09\u3002\u8d2d\u7269\u8f66\u6216\u7ed3\u8d26\u9875\u9762\u5e76\u4e0d\u80fd\u81ea\u52a8\u5173\u95ed\uff0c\u8fd9\u91cc\u6211\u4eec\u5c31\u53ef\u4ee5\u501f\u7528Effect\u6765\u89e3\u51b3\u95ee\u9898\u3002\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539<code>Cart.js<\/code>\u76f4\u63a5\u5411\u7ec4\u4ef6\u4e2d\u6dfb\u52a0\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>useEffect<\/em>(() =&gt; {<br>    if(ctx.totalAmount === 0) {<br>        setShowCheckout(false);<br>        setShowDetails(false);<br>    }<br>}, [ctx]);<\/pre>\n\n\n\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u5f53\u8d2d\u7269\u8f66\u4e2d\u7684\u5546\u54c1\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5c31\u4f1a\u89e6\u53d1useEffect\uff0c\u4ece\u800c\u68c0\u67e5\u5546\u54c1\u7684\u603b\u6570\u91cf\uff0c\u5982\u679c\u603b\u6570\u91cf\u4e3a0\u7684\u8bdd\u5c31\u4f1a\u5c06\u8d2d\u7269\u8f66\u8be6\u60c5\u9875\u548c\u7ed3\u8d26\u4e5f\u76f4\u63a5\u9690\u85cf\u3002<\/p>\n\n\n\n<p>\u9664\u4e86<code>Cart.js<\/code>\u4ee5\u5916\uff0cFilterMeals\u7ec4\u4ef6\u4e5f\u5b58\u5728\u4e00\u4e2a\u95ee\u9898\uff0c\u9996\u5148\uff0c\u8be5\u7ec4\u4ef6\u4e2d\u7684\u8868\u5355\u9879\u6211\u4eec\u5e76\u6ca1\u6709\u4f7f\u7528state\uff0c\u6240\u4ee5\u8fd9\u4e2a\u7ec4\u4ef6\u662f\u4e00\u4e2a\u975e\u53d7\u63a7\u7ec4\u4ef6\uff0c\u867d\u7136\u76ee\u524d\u770b\u6765\u6ca1\u4ec0\u4e48\u592a\u5927\u7684\u95ee\u9898\uff0c\u4f46\u662f\u6211\u4eec\u8fd8\u662f\u5e94\u8be5\u5904\u7406\u4e00\u4e0b\uff0c\u56e0\u4e3a\u53d7\u63a7\u7ec4\u4ef6\u4f7f\u7528\u65f6\u4f1a\u66f4\u52a0\u7684\u7075\u6d3b\uff0c\u53ef\u4ee5\u9002\u7528\u4e8e\u66f4\u591a\u7684\u573a\u666f\u3002\u5176\u6b21\u3001\u8be5\u7ec4\u4ef6\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u8fc7\u6ee4\u6c49\u5821\u7684\u5217\u8868\uff0c\u5f53\u7528\u6237\u8f93\u5165\u5173\u952e\u5b57\u65f6\u5b83\u53ef\u4ee5\u6839\u636e\u5173\u952e\u5b57\u7684\u5185\u5bb9\u5bf9\u98df\u7269\u5217\u8868\u8fdb\u884c\u8fc7\u6ee4\u3002\u95ee\u9898\u6b63\u5728\u4e8e\u6b64\uff0c\u7531\u4e8e\u6bcf\u6b21\u7528\u6237\u8f93\u5165\u90fd\u9700\u8981\u8fc7\u6ee4\uff0c\u8fd9\u5c31\u610f\u5473\u7740\u5b83\u7684\u8fc7\u6ee4\u9891\u7387\u8fc7\u9ad8\u4e86\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u7528\u6237\u8981\u8f93\u5165\u201c\u6c49\u5821\u201d\u8fd9\u4e2a\u5173\u952e\u5b57\uff0c\u4ed6\u9700\u8981\u4e00\u6b21\u8f93\u5165h-a-n-g-b-a-o\u4e03\u4e2a\u5b57\u6bcd\uff0c\u7531\u4e8e\u6bcf\u6b21\u8f93\u5165\u90fd\u4f1a\u89e6\u53d1\u4e00\u6b21\u8fc7\u6ee4\uff0c\u6240\u4ee5\u5728\u201c\u6c49\u5821\u201d\u6253\u51fa\u6765\u4e4b\u524d\uff0c\u5217\u8868\u5b8c\u5168\u662f\u4e00\u4e2a\u7a7a\u767d\u7684\u72b6\u6001\uff0c\u540c\u65f6\u65e0\u7528\u7684\u8fc7\u6ee4\u4e5f\u5bf9\u5e94\u7528\u7684\u6027\u80fd\u9020\u6210\u4e86\u4e00\u5b9a\u7684\u5f71\u54cd\u3002\u600e\u4e48\u529e\u5462\uff1f\u540c\u6837\u53ef\u4ee5\u4f7f\u7528Effect\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4fee\u6539FilterMeals\u4e2d\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [keyword, setKeyword] = <em>useState<\/em>('');<br><br><em>useEffect<\/em>(() =&gt; {<br>    const timer = <em>setTimeout<\/em>(() =&gt; {<br>        props.onFilter(keyword);<br>    }, 1000);<br><br>    return () =&gt; {<br>        <em>clearTimeout<\/em>(timer);<br>    };<br>}, [keyword]);<br><br>const inputChangeHandler = e =&gt; {<br>    setKeyword(e.target.value.trim());<br>};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>React\u7ec4\u4ef6\u6709\u90e8\u5206\u903b\u8f91\u90fd\u53ef\u4ee5\u76f4\u63a5\u7f16\u5199\u5230\u7ec4\u4ef6\u7684\u51fd\u6570\u4f53\u4e2d\u7684\uff0c\u50cf\u662f\u5bf9\u6570\u7ec4\u8c03\u7528filter\u3001map\u7b49\u65b9\u6cd5\uff0c\u50cf\u662f\u5224\u65ad\u67d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5648,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[200,202,199,201],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5646"}],"collection":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5646"}],"version-history":[{"count":12,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5646\/revisions"}],"predecessor-version":[{"id":5836,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5646\/revisions\/5836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/5648"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}