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 TableContainer API. The TableContainer is used to place the Table component inside it. It maintains the size and design of the Table adjusting to different screen sizes. A table is a component used to display a collection of data. The API provides a lot of functionality and we will learn to implement them.
Import TableContainer API:
import TableContainer from '@mui/material/TableContainer'; // or import { TableContainer } 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.
- sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles
- component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
Syntax: Create a TabelContainer component as follows:
<TableContainer component={Paper}> <Table sx={{ minWidth: 650 }}> ... </Table> </TableContainer>
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 TableContainer with a Table inside it.
App.js
import './App.css' import * as React from 'react' import Table from '@mui/material/Table' import TableBody from '@mui/material/TableBody' import TableCell from '@mui/material/TableCell' import TableContainer from '@mui/material/TableContainer' import TableHead from '@mui/material/TableHead' import TableRow from '@mui/material/TableRow' import Paper from '@mui/material/Paper' function createData(index = 0, tutorial = '' , link = '' ) { return { index, tutorial, link } } const rows = [ createData( 1, 'Data Strucutures' , ), createData( 2, 'Algorithms' , ), createData( 3, 'Competitive Programming' , ), ] function App() { return ( <div className= "App" > <div className= "head" style={{ width: 'fit-content' , margin: 'auto' , }} > <h1 style={{ color: 'green' , }} > neveropen </h1> <strong>React MUI TableContainer API</strong> </div> <TableContainer component={Paper}> <Table sx={{ minWidth: 650 }}> <TableHead> <TableRow> <TableCell>Sl. No.</TableCell> <TableCell align= "center" > Tutorial(Center Align)</TableCell> <TableCell>Link</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row, index) => ( <TableRow key={row.name}> <TableCell component= "th" scope= "row" > {row.index} </TableCell> <TableCell align= "center" > {row.tutorial}</TableCell> <TableCell> <a href={row.link} target= "_blank" > {row.link} </a> </TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer> </div> ) } export default App |
Output:
Example 2: In the following example, we have customized the TableContainer with a different background color using the sx field.
App.js
import './App.css' import * as React from 'react' import Table from '@mui/material/Table' import TableBody from '@mui/material/TableBody' import TableCell from '@mui/material/TableCell' import TableContainer from '@mui/material/TableContainer' import TableHead from '@mui/material/TableHead' import TableRow from '@mui/material/TableRow' import Paper from '@mui/material/Paper' function createData(index = 0, tutorial = '' , link = '' ) { return { index, tutorial, link } } const rows = [ createData( 1, 'Data Strucutures' , ), createData( 2, 'Algorithms' , ), createData( 3, 'Competitive Programming' , ), ] function App() { return ( <div className= "App" > <div className= "head" style={{ width: 'fit-content' , margin: 'auto' , }} > <h1 style={{ color: 'green' , }} > neveropen </h1> <strong>React MUI TableContainer API</strong> </div> <TableContainer component={Paper} sx={{ backgroundColor: 'lightgreen' , }} > <Table sx={{ minWidth: 650 }}> <TableHead> <TableRow> <TableCell>Sl. No.</TableCell> <TableCell align= "center" > Tutorial(Center Align)</TableCell> <TableCell>Link</TableCell> </TableRow> </TableHead> <TableBody> {rows.map((row, index) => ( <TableRow key={row.name}> <TableCell component= "th" scope= "row" > {row.index} </TableCell> <TableCell align= "center" > {row.tutorial}</TableCell> <TableCell> <a href={row.link} target= "_blank" > {row.link} </a> </TableCell> </TableRow> ))} </TableBody> </Table> </TableContainer> </div> ) } export default App |
Output:
Reference: https://mui.com/material-ui/api/table-container/