Open In App

React MUI PaginationItem API

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 PaginationItem API. The icon element is used to create different icons of different shapes and sizes with various colors. The PaginationItems are individual page numbers displayed. The API provides a lot of functionality and we will learn to implement them.



Import PaginationItem API

import PaginationItem from '@mui/material/PaginationItem';
// or
import { PaginationItem } 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.

CSS Rules:

Syntax: 

<Pagination
    page={page}
    onChange={handleChange}
    count={10}
    renderItem={(item) => (
        <PaginationItem
            components={{ 
                previous: ArrowBackIcon, 
                next: ArrowForwardIcon 
            }}
            {...item}
        />
    )}
/>

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 
npm install @emotion/styled @mui/lab @mui/icons-material

Project Structure: The project should look like the below:

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have created the custom arrows as PaginationItems.




import "./App.css";
import * as React from "react";
import Pagination from "@mui/material/Pagination";
import PaginationItem from "@mui/material/PaginationItem";
import Stack from "@mui/material/Stack";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import ArrowForwardIcon from "@mui/icons-material/ArrowForward";
import { Box, Typography } from "@mui/material";
function App() {
    const [page, setPage] = React.useState(1);
    const handleChange = (event, value) => {
        setPage(value);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI PaginationItem API</strong>
            </div>
            <br />
  
            <Box
                sx={{
                    margin: "auto",
                    width: "fit-content",
                    alignItems: "center",
                }}
            >
                <Typography fontSize={32} align="center">
                    Page: {page}
                </Typography>
                <Stack spacing={2}>
                    <Pagination
                        onChange={handleChange}
                        count={10}
                        renderItem={(item) => (
                            <PaginationItem
                                components={{ 
                                    previous: ArrowBackIcon, 
                                    next: ArrowForwardIcon 
                                }}
                                {...item}
                            />
                        )}
                    />
                </Stack>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Example 2: In the following example, we have created the PaginationItem of different sizes.




import "./App.css";
import * as React from "react";
import Pagination from "@mui/material/Pagination";
import PaginationItem from "@mui/material/PaginationItem";
import Stack from "@mui/material/Stack";
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
import ArrowForwardIcon from "@mui/icons-material/ArrowForward";
import { Box, Typography } from "@mui/material";
function App() {
    const [page, setPage] = React.useState(1);
    const handleChange = (event, value) => {
        setPage(value);
    };
  
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI PaginationItem API</strong>
            </div>
            <br />
            <Box
                sx={{
                    margin: "auto",
                    width: "fit-content",
                    alignItems: "center",
                }}
            >
                <Typography fontSize={32} align="center">
                    Page: {page}
                </Typography>
                <Stack spacing={2}>
                    <Pagination
                        onChange={handleChange}
                        count={10}
                        size="large"
                        renderItem={(item) => (
                            <PaginationItem
                                components={{
                                    previous: ArrowBackIcon,
                                    next: ArrowForwardIcon
                                }}
                                {...item}
                            />
                        )}
                    />
                    <Pagination
                        onChange={handleChange}
                        count={10}
                        size="medium"
                        renderItem={(item) => (
                            <PaginationItem
                                components={{
                                    previous: ArrowBackIcon,
                                    next: ArrowForwardIcon
                                }}
                                {...item}
                            />
                        )}
                    />
                    <Pagination
                        onChange={handleChange}
                        count={10}
                        size="small"
                        renderItem={(item) => (
                            <PaginationItem
                                components={{
                                    previous: ArrowBackIcon,
                                    next: ArrowForwardIcon
                                }}
                                {...item}
                            />
                        )}
                    />
                </Stack>
            </Box>
        </div>
    );
}
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/pagination-item/#main-content


Article Tags :