[英]How to make React Tabs component as reusable component?
import { useState } from "react";
export default function TabsComponent() {
const tabs = [
{ name: "Home", link: "#", content: "Home Content" },
{ name: "About", link: "#", content: "About Content" },
{ name: "Contact", link: "#", content: "Contact Content" },
];
const [openTab, setOpenTab] = useState("Home");
return (
<div>
<div className="container mx-auto">
<div className="flex flex-col items-center justify-center max-w-xl">
<ul className="flex space-x-2">
{tabs.map((tab) => (
<li key={tab.name}>
<a
href={tab.link}
onClick={() => setOpenTab(tab.name)}
className="inline-block px-4 py-2 text-gray-600 bg-white rounded shadow"
>
{tab.name}
</a>
</li>
))}
</ul>
<div className="p-3 mt-6 bg-white border">
{tabs.map((tab) => (
<div
key={tab.name}
className={
tab.name === openTab ? "d-block" : "d-none"
}
>
{tab.content}
</div>
))}
</div>
</div>
</div>
</div>
);
}
我是 React 的新手,我希望这个 React Tabs 功能组件可以重用。 如何解构整个组件以在应用程序中随处使用。
我是 React 的新手,我希望这个 React Tabs 功能组件可以重用。 如何解构整个组件以在应用程序中随处使用。
你做的很棒,只需将选项卡作为道具传递
像这样:
export default function TabsComponent({tabs}) {
const [openTab, setOpenTab] = useState(tabs[0]);
return (
<div>
<div className="container mx-auto">
<div className="flex flex-col items-center justify-center max-w-xl">
<ul className="flex space-x-2">
{tabs.map((tab) => (
<li key={tab.name}>
<a
href={tab.link}
onClick={() => setOpenTab(tab.name)}
className="inline-block px-4 py-2 text-gray-600 bg-white rounded shadow"
>
{tab.name}
</a>
</li>
))}
</ul>
<div className="p-3 mt-6 bg-white border">
{tabs.map((tab) => (
<div
key={tab.name}
className={
tab.name === openTab ? "d-block" : "d-none"
}
>
{tab.content}
</div>
))}
</div>
</div>
</div>
</div>
);
}
在其他组件中:
<TabsComponent tabs={[
{ name: "Home", link: "#", content: "Home Content" },
{ name: "About", link: "#", content: "About Content" },
{ name: "Contact", link: "#", content: "Contact Content" },
]} />
如果你想有单独的组件来单独处理它们,你可以将每个部分定义为组件
我做了某事你可以在这里看到: https ://codesandbox.io/s/material-ui-grid-demo-forked-ob4re4?file=/src/Tab.jsx
有几种方法可以做到这一点。
export default function TabsComponent({tabs}) {
//const tabs = ... no need for this now..
//rest of code the same
}
//call like
function Home() {
return <TabsComponent tabs={[
{ name: "Home", link: "#", content: "Home Content" },
{ name: "About", link: "#", content: "About Content" },
{ name: "Contact", link: "#", content: "Contact Content" }
]}/>
}
children
道具。export default function TabsComponent({children}) {
//const tabs = ... no need for this now..
const tabs = children;
//rest of code the same
}
//call like
function Home() {
return <TabsComponent>
{[
{ name: "Home", link: "#", content: "Home Content" },
{ name: "About", link: "#", content: "About Content" },
{ name: "Contact", link: "#", content: "Contact Content" }
]}
</TabsComponent>;
}
function Home() {
return <TabsComponent>
<TabSection name="Home" link="#" default>
Home Content
</TabSection>
<TabSection name="About" link="#">
About Content
</TabSection>
<TabSection name="Contact" link="#">
Contact Content
</TabSection>
</TabsComponent>;
}
我没有显示 option.3 的代码,因为它比传递一些道具要复杂得多,但我认为这是最好的选择,所以如果您感兴趣,请告诉我们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.