javascriptreactjsfrontenddestructuringreact-tabs

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>
    );
}

I'm new to React, I want this React Tabs functional component to be reusable. How to de-structure this entire component to use everywhere in the app.

I'm new to React, I want this React Tabs functional component to be reusable. How to de-structure this entire component to use everywhere in the app.

requirement


Solution

  • What you did is great, just pass tabs as props

    like this :

    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>
      );
    }
    

    and in other component :

       <TabsComponent  tabs={[
            { name: "Home", link: "#", content: "Home Content" },
            { name: "About", link: "#", content: "About Content" },
            { name: "Contact", link: "#", content: "Contact Content" },
          ]} />
    

    and if you want to have seperated component to handle them sepratly you can define each part as compnent

    I did sth you can see here : https://codesandbox.io/s/material-ui-grid-demo-forked-ob4re4?file=/src/Tab.jsx