How to make mui skeleton for table in react js?
October 28, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui skeleton for table in react js, you have to use skeleton
component with on TableCell component. it will make mui skeleton for table in React JS.
Today, I am going to show you, How to make mui skeleton for table in react js
Installation
Install the following packages to use mui skeleton in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import Material-UI skeleton.
- Use the skeleton Component
Step 1: Install MUI and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import Material-UI skeleton.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import React, { useState, useEffect } 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";
import { Box, Button, Skeleton } from "@mui/material";
Step 3: Use the skeleton Component.
A skeleton screen is an animated placeholder that simulates the layout of a website while data is being loaded.
<TableRow key={index}>
<TableCell component="th" scope="row">
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
</TableRow>
MUI material make mui skeleton for table example.
The below code is an example, you need to import skeleton
Component. Then, you have to use skeleton
component with on TableCell component. Then it will make mui skeleton for table in react js.
App.js
import React, { useState, useEffect } 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";
import { Box, Button, Skeleton } from "@mui/material";
export default function App() {
const { data, fetchData } = useFetch();
return (
<Box>
<Button
onClick={fetchData}
variant="outlined"
sx={{ marginBottom: "10px" }}
>
Fetch Data
</Button>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Name</TableCell>
<TableCell>Email</TableCell>
<TableCell>Phone</TableCell>
<TableCell>Website</TableCell>
</TableRow>
</TableHead>
<TableBody>
{!data ? (
<TableRowsLoader rowsNum={10} />
) : (
data?.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell>{row.email}</TableCell>
<TableCell>{row.phone}</TableCell>
<TableCell>{row.website}</TableCell>
</TableRow>
))
)}
</TableBody>
</Table>
</TableContainer>
</Box>TableCell
);
}
const TableRowsLoader = ({ rowsNum }) => {
return [...Array(rowsNum)].map((row, index) => (
<TableRow key={index}>
< component="th" scope="row">
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
<TableCell>
<Skeleton animation="wave" variant="text" />
</TableCell>
</TableRow>
));
};
const useFetch = () => {
const [data, setData] = useState();
const fetchData = async () => {
setData();
const response = await fetch("https://jsonplaceholder.typicode.com/users");
const newData = await response.json();
setTimeout(() => setData(newData), 2000);
};
useEffect(() => {
fetchData();
}, []);
return { data, fetchData };
};
In the above code example, I have used the @mui/material
component and make mui skeleton for table in react js.
Here, we are provided code sandbox links for the above program make mui skeleton for table in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍