How to create Image Slider in ReactJS ?
Last Updated :
26 Jul, 2024
Image Slider is a dynamic web element that displays a collection of images and has a slider to switch between the Images. It is the most common feature to show image collection in web applications or mobile apps.
Websites like Amazon.com, Flipkart.com, and many e-commerce sites use image sliders to show multiple images of products.
We can create an image slider in ReactJS using Material UI. It has this component available for us and it is very easy to integrate. In this tutorial, we will learn how to create image sliders from scratch using ReactJS.
Before learning how to build an image slider for a web application in ReactJS, you should know the prerequisites so that you can build an image slider without any doubts or issues.
Prerequisites to Create Image Slider in ReactJS
After learning these concepts, we can now create an image slider in ReactJS. The steps for creating an image slider are shown below.
Steps to Create Image Slider in ReactJS
Follow these easy and simple steps to build your first image slider in ReactJS. The steps are explained with ReactJS code and examples for better understanding.
Step 1: Create a data set for Image Collection
Store the image paths and labels in a JavaScript array for the image collection as given below.
const MyCollection = [
{
label: "First Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000010/1.png",
},
{
label: "Second Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000009/2.png",
},
{
label: "Third Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000008/3.png",
},
];
Step 2: Display the Images with img Tag
Simply use the img tag to display the image collection and use an alt attribute for alt text.
<img
src={MyCollection[index].imgPath}
alt={MyCollection[index].label}
/>
Step 3: Create the Image Slider
Use MUI Paper, Typography, and MobileStepper components and icons to design and create the slider structure. Import and use them from the Material UI library.
Step 4: Define the slider functions
Define a function to change the image index when the next button is clicked.
const goToNextPicture = () => {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
};
Creating Image Slider in ReactJS Example:
This example demonstrates a working React Image Slider using the MUI components.
JavaScript
// Filename - App.js
import React from "react";
import Button from "@material-ui/core/Button";
import MobileStepper from "@material-ui/core/MobileStepper";
import Paper from "@material-ui/core/Paper";
import KeyboardArrowRight from "@material-ui/icons/KeyboardArrowRight";
import Typography from "@material-ui/core/Typography";
import { useTheme } from "@material-ui/core/styles";
import KeyboardArrowLeft from "@material-ui/icons/KeyboardArrowLeft";
// Collection of images with their labels and paths
const MyCollection = [
{
label: "First Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000010/1.png",
},
{
label: "Second Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000009/2.png",
},
{
label: "Third Picture",
imgPath:
"https://media.geeksforgeeks.org/wp-content/uploads/20210208000008/3.png",
},
];
const App = () => {
const CollectionSize = MyCollection.length;
const theme = useTheme();
const [index, setActiveStep] = React.useState(0);
// Function to go to the next picture
const goToNextPicture = () => {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
};
return (
<div
style={{
marginLeft: "40%",
}}
>
<h2>How to Create Image Slider in ReactJS?</h2>
<div
style={{
maxWidth: 400,
flexGrow: 1,
}}
>
<Paper
square
elevation={0}
style={{
height: 50,
display: "flex",
paddingLeft: theme.spacing(4),
backgroundColor: theme.palette.background.default,
alignItems: "center",
}}
>
<Typography>{MyCollection[index].label}</Typography>
</Paper>
<img
src={MyCollection[index].imgPath}
style={{
height: 255,
width: "100%",
maxWidth: 400,
display: "block",
overflow: "hidden",
}}
alt={MyCollection[index].label}
/>
<MobileStepper
variant="text"
position="static"
index={index}
steps={CollectionSize}
nextButton={
<Button
size="small"
onClick={goToNextPicture}
disabled={index === CollectionSize - 1}
>
Next
{theme.direction !== "rtl" ? (
<KeyboardArrowRight />
) : (
<KeyboardArrowLeft />
)}
</Button>
}
/>
</div>
</div>
);
};
export default App;
Output:
Conclusion
Creating an image slider makes your web application more dynamic and interactive. This tutorial showed an easy process to create an image slider in ReactJS. We also created a live working image slider that you can use for your web applications.
There are also many options to customize the structure and appearance of the image slider according to your needs. We have also covered some basic customization options, but you can experiment with them to improve your website UX.
Similar Reads
How to crop images in ReactJS ?
Image cropping is a common requirement in web applications, especially when dealing with user-generated content, photo editing, or image manipulation. ReactJS, being a popular JavaScript library for building user interfaces, provides various techniques and libraries that can be used to implement ima
3 min read
How to create Tabs in ReactJS ?
Tabs make it easy to explore and switch between different views. Material UI for React has this component available for us and it is very easy to integrate. We can use Tabs Component in ReactJS using the following approach.Prerequisites:NPM & Node.jsReact JSMaterial UIwe have these approaches to
4 min read
How to create Price Range Selector in ReactJS?
Price Range Selector means the user is able to select the range between the given values. Material UI for React has this component available for us and it is very easy to integrate. Price Ranges Selectors is a very popular feature for filter the results on the basis of the range selected by the user
2 min read
How to create Loading Screen in ReactJS ?
Loading screen plays a major role in enhancing UX development. In this article, we are going to learn how we can create a Loading Screen in ReactJs. A loading screen is a picture shown by a computer program, often a video game, while the program is loading or initializing.PrerequisitesJavaScript and
2 min read
How to Set Background Images in ReactJS
Setting the background images in React improves the UI and user experience of web apps by making the appearance better. These images can be some shape or shade using color gradients. In this tutorial, we will look at different methods to apply background images to your react application. How to Set
4 min read
How to use Slide Component in ReactJS ?
Slide Component adds a Slide animation to a child element or component. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Slide Component in ReactJS using the following approach.Prerequisites:NodeJS or NPMReactJSMaterial UISteps to Create the
3 min read
How to create Video Player in ReactJS ?
We are going to learn how we can create a Video Player in React JS. A video player is a kind of media player for playing back digital video data. React is a free and open-source front-end JavaScript library for building user interfaces or UI components.Prerequisite:React JSPhases of JavaScript Event
2 min read
How to add Input Slider in React JS ?
We will learn how to add an input slider in React JS. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It was introduced by Facebook and a community of individual developers and companies. Prerequisites:Node JS or NPMReact JSReact useStateAp
2 min read
How To Create a Website in ReactJS?
ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS.PrerequisitesNPM & Node.jsR
5 min read
Create Image Slider on Hover in Tailwind CSS
This article aims to demonstrate How to create an Image Slide on Hover Effect using Tailwind CSS. The idea behind this is that when user hovers the cursor over the given image, it smoothly slides out of view, revealing additional content or providing an interactive experience.Image Slide on Hover Ef
2 min read