Open In App

React MUI Tab API

MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we will discuss the React MUI Tab API. The Tabs are used to make navigation easier and more useful. We can easily switch between the views. A Tab is a single item in the Tabs that appear as a clickable button. The API provides a lot of functionality and we will learn to implement them.



Import Tab API:

import Tab from '@mui/material/Tab';
// or
import { Tab } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.



Syntax: Create a Tab inside Tabs as follows:

<Tabs value={value} onChange={handleChange} 
    aria-label="basic tabs example">
    <Tab label="Item One"/>
    <Tab label="Item Two"/>
    <Tab label="Item Three"/>
</Tabs>

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have three Tabs.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Tab from "@mui/material/Tab";
import TabContext from "@mui/lab/TabContext";
import TabList from "@mui/lab/TabList";
import TabPanel from "@mui/lab/TabPanel";
function App() {
    const [value, setValue] = React.useState("1");
  
    const handleChange = (event, newValue) => {
        setValue(newValue);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
            </div>
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <strong>React MUI Tab API</strong>
            </div>
            <br />
            <div
                style={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                }}
            >
                <Box sx={{ width: "100%", typography: "body1" }}>
                    <TabContext value={value}>
                        <Box sx={{ borderBottom: 1, borderColor: "divider" }}>
                            <TabList onChange={handleChange}>
                                <Tab label="Tutorial 1" value="1" />
                                <Tab label="Tutorial 2" value="2" />
                                <Tab label="Tutorial 3" value="3" />
                            </TabList>
                        </Box>
                        <TabPanel value="1">Data Structures</TabPanel>
                        <TabPanel value="2">Algorithms</TabPanel>
                        <TabPanel value="3">Web Development</TabPanel>
                    </TabContext>
                </Box>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

Example 2: In the following example, we have applied different icons on the tabs.




import "./App.css";
import * as React from "react";
import Box from "@mui/material/Box";
import Tab from "@mui/material/Tab";
import TabContext from "@mui/lab/TabContext";
import TabList from "@mui/lab/TabList";
import TabPanel from "@mui/lab/TabPanel";
import { Android, Phone, Watch } from "@mui/icons-material";
function App() {
    const [value, setValue] = React.useState("1");
  
    const handleChange = (event, newValue) => {
        setValue(newValue);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
            </div>
            <div
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <strong>React MUI Tab API</strong>
            </div>
            <br />
            <div
                style={{
                    margin: "auto",
                    display: "flex",
                    justifyContent: "space-evenly",
                }}
            >
                <Box sx={{ width: "100%", typography: "body1" }}>
                    <TabContext value={value}>
                        <Box sx={{ borderBottom: 1, 
                            borderColor: "divider" }}>
                            <TabList onChange={handleChange}>
                                <Tab icon={<Android />} 
                                    label="Tutorial 1" value="1" />
                                <Tab icon={<Phone />} 
                                    label="Tutorial 2" value="2" />
                                <Tab icon={<Watch />} 
                                    label="Tutorial 3" value="3" />
                            </TabList>
                        </Box>
                        <TabPanel value="1">Data Structures</TabPanel>
                        <TabPanel value="2">Algorithms</TabPanel>
                        <TabPanel value="3">Web Development</TabPanel>
                    </TabContext>
                </Box>
            </div>
        </div>
    );
}
  
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/tab/


Article Tags :