Open In App

React MUI ImageListItem 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 ImageListItem API. The ImageList displays a collection of images in a structured grid format. ImageListItem contains a single image. The API provides a lot of functionality and we will learn to implement them.



Import ImageListItem API:

import ImageListItem from '@mui/material/ImageListItem';
// or
import { ImageListItem } 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: Create an ImageListItem as follows:

<ImageListItem cols={2} rows={1}>
    <img src="#" loading="lazy"/>
</ImageListItem>

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 an ImageListItem inside ImageList.




import "./App.css";
import * as React from "react";
import ImageList from "@mui/material/ImageList";
import ImageListItem from "@mui/material/ImageListItem";
function srcset(image, size, rows = 1, cols = 1) {
    return {
        src: `${image}?w=${size * cols}
    &h=${size * rows}&fit=crop&auto=format`,
        srcSet: `${image}?w=${size * cols}&h
            =${size * rows
            }&fit=crop&auto=format&dpr=2 2x`,
    };
}
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI ImageListItem API</strong>
            </div>
            <br />
            <ImageList
                variant="quilted"
                sx={{ width: 500, height: 250, margin: "auto" }}
                cols={4}
                rowHeight={121}
            >
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                              
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
                              
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
            </ImageList>
        </div>
    );
}
export default App;

Output:

 

Example 2: In the following example, we have ImageListItem with different cols and rows size.




import "./App.css";
import * as React from "react";
import ImageList from "@mui/material/ImageList";
import ImageListItem from "@mui/material/ImageListItem";
function srcset(image, size, rows = 1, cols = 1) {
    return {
        src: `${image}?w=${size * cols}
     &h=${size * rows}&fit=crop&auto=format`,
        srcSet: `${image}?w=${size * cols}&h=${size * rows
            }&fit=crop&auto=format&dpr=2 2x`,
    };
}
  
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1
                    style={{
                        color: "green",
                    }}
                >
                    GeeksforGeeks
                </h1>
                <strong>React MUI ImageListItem API</strong>
            </div>
            <br />
            <ImageList
                variant="quilted"
                sx={{ width: 500, height: 450, margin: "auto" }}
                cols={4}
                rowHeight={121}
            >
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={2} rows={1}>
                    <img
                        {...srcset(
              121,
                            2,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
                <ImageListItem cols={1} rows={1}>
                    <img
                        {...srcset(
              121,
                            1,
                            1
                        )}
                        loading="lazy"
                    />
                </ImageListItem>
            </ImageList>
        </div>
    );
}
export default App;

Output:

 

Reference: https://mui.com/material-ui/api/image-list-item/


Article Tags :