Open In App

React MUI Tab API

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • children (node): It is a component similar to the table row.
  • classes (Object): Override the existing styles or add new styles to the component.
  • component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
  • sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.
  • disabled(bool): If true, the component is disabled.
  • disableFocusRipple(bool): If true, the keyboard focus ripple effect is disabled.
  • disableRipple(bool): If true, the ripple effect is disabled.
  • icon(element): The icon to display.
  • iconPosition(bottom/end/start/top):  It sets the position of the icon relative to the label.
  • label(node): It is the label of the tab. 
  • value(any): We can provide any value or the Tabs will use by index.
  • wrapped(bool): If true, the labels appear in a single row.

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.

App.js




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.

App.js




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/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads