Open In App

React MUI TabContext 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 TabPanel API. The Tabs are used to make navigation easier and more useful. We can easily switch between the views. The TabContext is used to hold the index of the current tab. The API provides a lot of functionality and we will learn to implement them.

Import TabContext API:

import TabContext from '@mui/lab/TabContext';
// or
import { TabContext } from '@mui/lab';

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.
  • value(string): The currently selected tab index value.

Syntax: Use the TabContext in Tabs as follows:

<TabContext value={value}>
    ...
</TabContext>

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 a TabContext with Tabs inside.

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 TabContext 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 will be modifying the value provided in the TabContext using mod 3 +1 so that a different tab is selected.

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 TabContext API</strong>
      </div>
      <br />
      <div
        style={{
          margin: "auto",
          display: "flex",
          justifyContent: "space-evenly",
        }}
      >
        <Box sx={{ width: "100%", typography: "body1" }}>
          <TabContext value={((parseInt(value) % 3) + 1).toString()}>
            <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

 

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



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

Similar Reads