{"id":52200,"date":"2024-12-03T10:38:59","date_gmt":"2024-12-03T02:38:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52200\/"},"modified":"2024-12-03T10:38:59","modified_gmt":"2024-12-03T02:38:59","slug":"react-%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95-%e6%8f%8f%e8%bf%b0%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52200\/","title":{"rendered":"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732003970673c488233b23.jpg\" class=\"aligncenter\" title=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5\u63d2\u56fe\" alt=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5\u63d2\u56fe\" \/><\/p>\n<ul>\n<li>\n<p>\u5f53\u6211\u6d4b\u8bd5\u4e00\u4e9b\u5f53\u524d\u4ee3\u7801\u65f6\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528describe\u51fd\u6570\u5c06\u4ee3\u7801\u5206\u7ec4\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u8ba1\u6570\u5668\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u30fbsrc\/example.jsx<\/p>\n<pre>import counter from \".\/components\/counter\";\n\nconst example = () =&gt; {\n  const origincount = 0;\n\n  return &lt;counter origincount={origincount}&gt;&lt;\/counter&gt;;\n};\n\nexport default example;\n\n<\/pre>\n<p>\u30fbsrc\/components\/counter.jsx<\/p>\n<pre>import { usestate } from \"react\";\n\nconst counter = (props) =&gt; {\n  const { origincount } = props;\n  const [count, setcount] = usestate(origincount);\n\n  const countup = () =&gt; {\n    setcount(count + 1);\n  };\n\n  const countdown = () =&gt; {\n    setcount(count - 1);\n  };\n\n  const countclear = () =&gt; {\n    setcount(0);\n  };\n\n  return (\n    &lt;div&gt;\n      &lt;h2&gt;counter test&lt;\/h2&gt;\n      &lt;div&gt;\n        &lt;span&gt;current count:{count}&lt;\/span&gt;\n      &lt;\/div&gt;\n      &lt;div&gt;\n        &lt;button onclick={countup}&gt;countup&lt;\/button&gt;\n        &lt;button onclick={countdown}&gt;countdown&lt;\/button&gt;\n        &lt;button onclick={countclear}&gt;clear&lt;\/button&gt;\n      &lt;\/div&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default counter;\n\n<\/pre>\n<p>\u30fbsrc\/components\/counter.test.jsx<\/p>\n<pre>import { fireEvent, render, screen } from \"@testing-library\/react\";\nimport Counter from \".\/Counter\";\n\n\/\/A group for initial test\ndescribe(\"Counter\", () =&gt; {\n describe(\"Check the initial display\", () =&gt; {\n\n\u3000\u3000\/\/ \u2460\u3000A Confirming the Initial State\n    test(\"Whether \n    test(\"Whether the current count is 0 or not\", () =&gt; {\n      render(&lt;Counter originCount={0} \/&gt;);\n\n      const spanElBeforeUpdate = screen.getByText(\"Current count:0\");\n      expect(spanElBeforeUpdate).toBeInTheDocument();\n    });\n  });\n\n  \/\/A group for actions tests\n  describe(\"Control buttons\", () =&gt; {\n\n    \/\/ \u2460\u3000count up\n    test(\"Whether the current count changes into 1 or not, in case the \n      countup button is clicked\", () =&gt; {\n      render(&lt;Counter originCount={0} \/&gt;);\n\n      const spanElBeforeUpdate = screen.getByText(\"Current count:0\");\n      expect(spanElBeforeUpdate).toBeInTheDocument();\n\n      const btn = screen.getByRole(\"button\", { name: \"Countup\" });\n      fireEvent.click(btn);\n\n      const spanEl = screen.getByText(\"Current count:1\");\n      expect(spanEl).toBeInTheDocument();\n    });\n\u3000\u3000\n\u3000\u3000\/\/ \u2461\u3000count down \n    test(\"Whether the current count  changes into -1 or not, in case the \n      countdown button is clicked \", () =&gt; {\n      render(&lt;Counter originCount={0} \/&gt;);\n\n      const spanElBeforeUpdate = screen.getByText(\"Current count:0\");\n      expect(spanElBeforeUpdate).toBeInTheDocument();\n\n      const btn = screen.getByRole(\"button\", { name: \"Countdown\" });\n      fireEvent.click(btn);\n\n      const spanEl = screen.getByText(\"Current count:-1\");\n      expect(spanEl).toBeInTheDocument();\n    });\n\n\u3000\u3000\/\/ \u2462\u3000count clear \n    test(\"Whether the current count changes into 0 or not, in case the \n      clear button is clicked \", () =&gt; {\n      render(&lt;Counter originCount={0} \/&gt;);\n\n      const spanElBeforeUpdate = screen.getByText(\"Current count:0\");\n      expect(spanElBeforeUpdate).toBeInTheDocument();\n\n      const btn = screen.getByRole(\"button\", { name: \"Clear\" });\n      fireEvent.click(btn);\n\n      const spanEl = screen.getByText(\"Current count:0\");\n      expect(spanEl).toBeInTheDocument();\n    });\n  });\n});\n\n<\/pre>\n<p>\u30fb\u8ba1\u6570<\/p>\n<p>\u30fb\u5012\u8ba1\u65f6<br \/> \u30fb\u5012\u8ba1\u65f6<br \/> \u30fb\u6210\u529f<br \/> \u30fb\u5931\u8d25<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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   \u5982\u4f55\u67e5\u770b\u7535\u8111\u7684CPU\u4fe1\u606f\u5e76\u8fdb\u884c\u6027\u80fd\u76d1\u63a7\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5 \u6536\u85cf \u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u63cf\u8ff0\u6d4b\u8bd5\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01 \u5f53\u6211\u6d4b\u8bd5\u4e00\u4e9b\u5f53\u524d\u4ee3\u7801\u65f6\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528describe\u51fd\u6570\u5c06\u4ee3\u7801\u5206\u7ec4\u3002 \u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2a\u8ba1\u6570\u5668\u5e94\u7528\u7a0b\u5e8f\u3002 \u30fbsrc\/example.jsx import counter from &#8220;.\/components\/counter&#8221;; const example = () =&gt; { const origincount = 0; return &lt;counter origincount={origincount}&gt;&lt;\/counter&gt;; }; export default example; \u30fbsrc\/components\/counter.jsx import { usestate } from &#8220;react&#8221;; const counter = (props) =&gt; { const { origincount } = props; const [count, setcount] = usestate(origincount); const countup [&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-52200","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52200","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=52200"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52200\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}