Open In App

React MUI TableBody API

Last Updated : 04 Aug, 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 are going to discuss the React MUI TableBody API. TableBody is used to place the TableRows and the TableCells of the tables that is the data. 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 TableBody 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.
  • 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

Syntax: Create a TableBody as follows:

<TableBody>
  {rows.map((row, index) => (
    <TableRow key={row.name}>
      <TableCell align="center">{row.name}</TableCell>
    </TableRow>
  ))}
</TableBody>

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 TableBody with its contents.

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',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TableBody 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 TableBody 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',
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI TableBody 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
                        sx={{
                            backgroundColor: 'lightyellow',
                        }}
                    >
                        {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-body/



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

Similar Reads