{"id":52816,"date":"2024-12-03T11:59:23","date_gmt":"2024-12-03T03:59:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52816\/"},"modified":"2024-12-03T11:59:23","modified_gmt":"2024-12-03T03:59:23","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-react-%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e5%a4%9a-tab-%e9%a1%b5%e7%bb%84%e4%bb%b6%ef%bc%8c%e4%b8%94%e6%95%b0%e6%8d%ae%e4%b8%8d%e5%9b%ba%e5%ae%9a%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52816\/","title":{"rendered":"\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f <\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f \u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323567526741aa904af8e.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5b9e\u73b0\u591a tab \u9875\u52a8\u6001\uff0c\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u5177\u6709\u52a8\u6001 tab \u9875\u7684\u591a tab \u9875\u7ec4\u4ef6\uff0c\u5176\u4e2d\u6570\u636e\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 React \u7684 useState \u548c useEffect \u94a9\u5b50\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002<\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a useState \u94a9\u5b50\u6765\u5b58\u50a8 tab \u9875\u6570\u636e\uff1a<\/p>\n<pre>const [tabs, setTabs] = useState([]);<\/pre>\n<p>\u7136\u540e\uff0c\u4f7f\u7528 useEffect \u94a9\u5b50\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u83b7\u53d6\u6570\u636e\uff1a<\/p>\n<pre>useEffect(() =&gt; {\n  const fetchData = async () =&gt; {\n    const data = await fetch(\"data.json\");\n    const tabs = await data.json();\n    setTabs(tabs);\n  };\n\n  fetchData();\n}, []);<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528 map \u8fed\u4ee3 tabs \u6570\u636e\uff0c\u5e76\u4e3a\u6bcf\u4e2a tab \u9875\u521b\u5efa JSX \u5143\u7d20\uff1a<\/p>\n<pre>{tabs.map(tab =&gt; (\n  &lt;Tab key={tab.id} title={tab.title} content={tab.content} \/&gt;\n))}<\/pre>\n<p>\u6700\u540e\uff0c\u521b\u5efa\u4e00\u4e2a Tab \u7ec4\u4ef6\u8d1f\u8d23\u5448\u73b0\u5355\u4e2a tab \u9875\u7684\u5185\u5bb9\uff1a<\/p>\n<pre>const Tab = ({ title, content }) =&gt; (\n  &lt;div&gt;\n    &lt;div&gt;{title}&lt;\/div&gt;\n    &lt;div&gt;{content}&lt;\/div&gt;\n  &lt;\/div&gt;\n);<\/pre>\n<p>\u8fd9\u4e2a\u5b9e\u73b0\u53ef\u4ee5\u8ba9\u4f60\u57fa\u4e8e\u52a8\u6001\u83b7\u53d6\u7684\u6570\u636e\u521b\u5efa\u591a tab \u9875\u7ec4\u4ef6\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u7b54\u6848\u53c2\u8003\u4e86\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u5b9e\u73b0\uff1ahttps:\/\/blog.csdn.net\/wgqjiayou\/article\/details\/118751868<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f \u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u6839\u636e\u6570\u503c\u5feb\u901f\u5b9a\u4f4d\u5bf9\u5e94\u533a\u95f4\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a\u5982\u4f55\u7528 React \u5b9e\u73b0\u52a8\u6001\u591a Tab \u9875\u7ec4\u4ef6\uff0c\u4e14\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f \u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u5982\u4f55\u5b9e\u73b0\u591a tab \u9875\u52a8\u6001\uff0c\u6570\u636e\u4e0d\u56fa\u5b9a\uff1f \u95ee\u9898\uff1a \u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u5177\u6709\u52a8\u6001 tab \u9875\u7684\u591a tab \u9875\u7ec4\u4ef6\uff0c\u5176\u4e2d\u6570\u636e\u4e0d\u662f\u56fa\u5b9a\u7684\uff1f \u7b54\u6848\uff1a \u53ef\u4ee5\u4f7f\u7528 React \u7684 useState \u548c useEffect \u94a9\u5b50\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a useState \u94a9\u5b50\u6765\u5b58\u50a8 tab \u9875\u6570\u636e\uff1a const [tabs, setTabs] = useState([]); \u7136\u540e\uff0c\u4f7f\u7528 useEffect \u94a9\u5b50\u5728\u7ec4\u4ef6\u6302\u8f7d\u65f6\u83b7\u53d6\u6570\u636e\uff1a useEffect(() =&gt; { const fetchData = async () =&gt; { const data = await [&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-52816","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52816","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=52816"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52816\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}