Open In App

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



Import ImageList API

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

<ImageList sx={{ width: 500, height: 250, margin: "auto" }}
    cols={4} variant="quilted">
  <ImageListItem cols={1} rows={1}>
    <img
 src="#" loading="lazy"/>
  </ImageListItem>
</ImageList>

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 @mui/icons-material

Project Structure: The project structure should look like the below:

 

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a standard variant ImageList component




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 ImageList API</strong>
            </div>
            <br />
            <ImageList
                sx={{ width: 500, height: 250, margin: "auto" }}
                cols={4}
                variant="quilted"
                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 a woven variant of 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 ImageList API</strong>
            </div>
            <br />
            <ImageList
                sx={{ width: 500, height: 450, margin: "auto" }}
                variant="woven"
                cols={3}
                gap={8}
            >
                <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>
                <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/


Article Tags :