How to useMaterial UI Tab component in ReactJS

To create Tabs in React we can also use the Tab components provided by material UI. we will pass the parameters like label, content and disable to create the required tabs on the page.

Step to install MUI: After creating the ReactJS application, Install the material-ui modules using the following command:

npm install @material-ui/core

The updated dependencies after installing required packages

{
"dependencies": {
"@material-ui/core": "^4.12.4",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}

Example: This example implements multiple tabs using the MUI tab component.

Javascript




import React from "react";
import Paper from "@material-ui/core/Paper";
import Tab from "@material-ui/core/Tab";
import Tabs from "@material-ui/core/Tabs";
 
const App = () => {
    const [value, setValue] = React.useState(2);
 
    return (
        <div
            style={{
                marginLeft: "40%",
            }}
        >
            <h2>How to Create Tabs in ReactJS?</h2>
            <Paper square>
                <Tabs
                    value={value}
                    textColor="primary"
                    indicatorColor="primary"
                    onChange={(event, newValue) => {
                        setValue(newValue);
                    }}
                >
                    <Tab label="Active TAB One" />
                    <Tab label="Active TAB Two" />
                    <Tab label="Disabled TAB!" disabled />
                    <Tab label="Active Tab Three" />
                </Tabs>
                <h3>TAB NO: {value} clicked!</h3>
            </Paper>
        </div>
    );
};
 
export default App;


Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output.



How to create Tabs in ReactJS ?

Tabs make it easy to explore and switch between different views. Material UI for React has this component available for us and it is very easy to integrate. We can use Tabs Component in ReactJS using the following approach.

Similar Reads

Prerequisites:

NPM & Node.js React JS Material UI...

Steps to create React Application And Installing Module:

Step 1: Create a React application using the following command:...

Approach 1: Using custom Tab component with active parameter

We will create custom tab components with the label as parameter and import to create multiple tab example. We will use CSS properties to show and hide the tab content....

Approach 2: Using Material UI Tab component

...