{"id":53250,"date":"2024-12-03T13:55:46","date_gmt":"2024-12-03T05:55:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53250\/"},"modified":"2024-12-03T13:55:46","modified_gmt":"2024-12-03T05:55:46","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-%e7%94%a8%e6%88%b7%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53250\/","title":{"rendered":"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<ul>\n<li>\u5f53\u6211\u6d4b\u8bd5\u7528\u6237\u4e8b\u4ef6\u65f6\uff0c\u6211\u4f7f\u7528react-testing-library\u7684fireevent\u51fd\u6570\u3002<\/li>\n<\/ul>\n<p>\u30fbsrc\/example.js<\/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\/commponets\/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  return (\n    &lt;div&gt;\n      &lt;h2&gt;a test of counter&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;\/div&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default counter;\n<\/pre>\n<p>\u30fbsrc\/commponets\/counter.test.jsx<\/p>\n<pre>import { render, screen, fireEvent } from \"@testing-library\/react\";\nimport Counter from \".\/Counter\";\n\ntest(\"Whether the current count counts up or not, in case the countUp button is clicked \", () =&gt; {\n  const { debug } = render(&lt;Counter originCount={0} \/&gt;);\n\n\/\/test the initial state.\n  const spanElBeforUpdate = screen.getByText(\"Current count:0\");\n  expect(spanElBeforUpdate).toBeInTheDocument();\n\n\/\/test the user event. In this case, a click event.\n  const btn = screen.getByRole(\"button\", { name: \"Countup\" });\n  fireEvent.click(btn);\n\n\/\/test the state which is after clicked button.\n  const spanEl = screen.getByText(\"Current count:1\");\n  expect(spanEl).toBeInTheDocument();\n});\n\n<\/pre>\n<p>\u30fb\u6210\u529f<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173277902667481c129500a.jpg\" class=\"aligncenter\" title=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u63d2\u56fe\" alt=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u63d2\u56fe\" \/><\/p>\n<p>\u30fb\u5931\u8d25<br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/173277902667481c1298561.jpg\" class=\"aligncenter\" title=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u63d2\u56fe1\" alt=\"React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u63d2\u56fe1\" \/><\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\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   \u5929\u9a6c\u5fae\u7535\u5b50\u201c\u4e00\u79cd\u53ef\u6298\u53e0\u663e\u793a\u88c5\u7f6e\u3001\u7535\u5b50\u8bbe\u5907\u53ca\u63a7\u5236\u65b9\u6cd5\u201d\u4e13\u5229\u83b7\u6388\u6743\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>React \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6 \u6536\u85cf IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aReact \u57fa\u7840\u77e5\u8bc6~\u5355\u5143\u6d4b\u8bd5\/\u7528\u6237\u4e8b\u4ef6\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 \u5f53\u6211\u6d4b\u8bd5\u7528\u6237\u4e8b\u4ef6\u65f6\uff0c\u6211\u4f7f\u7528react-testing-library\u7684fireevent\u51fd\u6570\u3002 \u30fbsrc\/example.js 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\/commponets\/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-53250","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53250","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=53250"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53250\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}