Blogs

Tools

Quick Links

Mridul.tech

Creating a Stunning Image Gallery using React Image Gallery

Mridul Panda

Mridul Panda

Apr 19, 2023

·

5 Min Read

Creating a Stunning Image Gallery using React Image Gallery
react image gallery

When it comes to building a website, a visually appealing image gallery can make a huge difference in the overall user experience. React Image Gallery is a popular and flexible Reactjs component library that can help you create a stunning gallery for your website. In this article, we will explore the features of React Image Gallery and show you how to use it to create a beautiful and responsive image gallery.

If you’re looking for full code You can find it on GitHub

What is React Image Gallery?

React Image Gallery is a versatile React component library that provides a simple and intuitive interface for creating beautiful image galleries. It offers a wide range of customization options, making it suitable for different types of websites.

Features of React Image Gallery

Here are some of the features that make React Image Gallery stand out:

  • Responsive design: The style and dimensions of React Picture Gallery are automatically adjusted depending on the screen size of the device because it is made to be responsive.
  • Customizable layout: The component library provides a flexible layout that can be customized to fit your website design.
  • Touch and swipe support: It supports touch and swipes gestures, making it easy to navigate through the images on mobile devices.
  • Keyboard navigation: The component library also supports keyboard navigation, allowing users to navigate through the images using the arrow keys.
  • Lazy loading: It supports lazy loading of images, which means that images are loaded only when they are needed, reducing the page load time.

Also Read: Next JS vs React: Which One to Use for Your Next Web Project?

Getting started with React Image Gallery

We will install the react-image-gallery package via npm with the following command –

npm install react-image-gallery

Once you have installed React Image Gallery, you can import it into your project:

import ImageGallery from 'react-image-gallery';

Next, you can create an array of images to display in the gallery, We are using images from pexels.com

const images = [
  {
    original:
      "https://images.pexels.com/photos/831889/pexels-photo-831889.jpeg?auto=compress&cs=tinysrgb&w=1280",
    thumbnail:
      "https://images.pexels.com/photos/831889/pexels-photo-831889.jpeg?auto=compress&cs=tinysrgb&w=600",
    description: "Description 1",
  },
  {
    original:
      "https://images.pexels.com/photos/1683492/pexels-photo-1683492.jpeg?auto=compress&cs=tinysrgb&w=1280",
    thumbnail:
      "https://images.pexels.com/photos/1683492/pexels-photo-1683492.jpeg?auto=compress&cs=tinysrgb&w=600",
    description: "Description 2",
  },
  {
    original:
      "https://images.pexels.com/photos/1630128/pexels-photo-1630128.jpeg?auto=compress&cs=tinysrgb&w=1280",
    thumbnail:
      "https://images.pexels.com/photos/1630128/pexels-photo-1630128.jpeg?auto=compress&cs=tinysrgb&w=600",
    description: "Description 3",
  },
  {
    original:
      "https://images.pexels.com/photos/2273664/pexels-photo-2273664.jpeg?auto=compress&cs=tinysrgb&w=1280",
    thumbnail:
      "https://images.pexels.com/photos/2273664/pexels-photo-2273664.jpeg?auto=compress&cs=tinysrgb&w=600",
    description: "Description 4",
  },
  {
    original:
      "https://images.pexels.com/photos/572740/pexels-photo-572740.jpeg?auto=compress&cs=tinysrgb&w=1280",
    thumbnail:
      "https://images.pexels.com/photos/572740/pexels-photo-572740.jpeg?auto=compress&cs=tinysrgb&w=600",
    description: "Description 5",
  },
];

Finally, you can render the image gallery component with the images array:

<ImageGallery items={images} />

You can also customize the appearance and behaviour of the gallery by passing in additional props. For example, you can set the thumbnail width and height or enable autoplay:

<ImageGallery
  items={images}
  showThumbnails={false}
  showFullscreenButton={false}
  autoPlay={true}
  slideInterval={5000}
/>

Customizing Image Gallery

React Image Gallery provides a wide range of customization options to make the gallery fit your website design. Here are some of the options you can customize:

  • Thumbnail position: You can choose where to position the thumbnail images relative to the main image.
  • Slide duration: You can specify the duration of each slide in milliseconds.
  • Slide interval: You can specify the interval between slides in milliseconds.
  • Infinite sliding: You can enable infinite sliding, allowing the gallery to loop through the images continuously.
  • Show full-screen button: You can choose to show or hide the full-screen button.

If you want to further customize your gallery, ReactJS Image Gallery provides a range of callbacks and event handlers that you can use to control the behaviour of the gallery.

Adding Image Lazyload

You can use the onImageLoad callback to perform an action when an image is loaded:

function handleImageLoad(event) {
  console.log('Image loaded:', event.target);
}

<ImageGallery items={images} onImageLoad={handleImageLoad} />

Customizing Image Render

You can also use the renderItem prop to customize the appearance of each image:

function renderItem(item) {
  return (
    <div className='image-gallery-image'>
      <img src={item.original} alt={item.description} />
      <div className='image-gallery-description'>{item.description}</div>
    </div>
  );
}

<ImageGallery items={images} renderItem={renderItem} />

Customizing Thumbnail Position

You can also use the thumbnailPosition prop to change the position of the thumbnail strip:

<ImageGallery items={images} thumbnailPosition='left' />

Also Read: How to use GitHub GraphQL API in React or Next JS

Tips for optimizing React Image Gallery

To ensure that your image gallery loads quickly and smoothly, here are some tips to keep in mind:

  • Optimize your images: Use optimized images to reduce their file size and improve loading time.
  • Lazy loading: You can also enable lazy loading. This will load images only when they are needed.
  • Use CDN: Use a content delivery network (CDN) to speed up the delivery of images and reduce server load time.
  • Compress your code: Compress your code to reduce the size of your JavaScript files and improve page load time.

Frequently Asked Questions (FAQs)

Q: Can I use React Image Gallery with other React components?

A: Yes, React Image Gallery is designed to be used with other React components.

Q: Can I customize the styles of the image gallery?

A: Yes, React Image Gallery provides a wide range of customization options, including customizing the styles of the gallery.

Q: Can I add custom animations to the image gallery?

A: Yes, React Image Gallery supports custom animations using CSS transitions or animations.

Conclusion

ReactJS Image Gallery is a powerful and versatile component that makes it easy to create beautiful and responsive image galleries for your website. With a wide range of customization options and features, you can easily create a gallery that will impress your website and visitors. Whether you’re building a portfolio, an e-commerce site, or a personal blog, ReactJS Image Gallery is a great tool to have in your development arsenal. So go ahead, try it out, and see what amazing galleries you can create!

You may also like

React Hooks Cheatsheet

React Hooks Cheatsheet

Jan 21, 2024

·

4 Min Read

React, a popular JavaScript library for building user interfaces, introduced Hooks to make functional components more powerful and expressive. Let’s dive into the React Hooks Cheatsheet to understand how they enhance the development experience. React Hooks Cheatsheet In the ever-evolving landscape of web development, React Hooks have become indispensable for building dynamic and efficient user […]

Read More

React Emoji Picker: Add Emoji Picker in a React App

React Emoji Picker: Add Emoji Picker in a React App

May 18, 2023

·

3 Min Read

Emojis have become a staple in modern communication, with people using them to convey emotions and ideas in various contexts. As such, it’s important for developers to provide users with an easy and intuitive way to input emojis into their applications. This is where the React Emoji Picker comes in handy. This tool simplifies the […]

Read More

Top 5 Platforms to Host React Apps for Free

Top 5 Platforms to Host React Apps for Free

Apr 11, 2023

·

7 Min Read

Learn about the best platforms to host your React apps for free. This guide will help you choose the right platform that suits your needs.

Read More

How to Upload Images to Cloudinary With React JS

How to Upload Images to Cloudinary With React JS

Apr 05, 2023

·

6 Min Read

Are you struggling to upload images in your React JS application? Are you tired of the tedious process of configuring file uploads and handling image storage? If so, you are not alone. Many developers find image management in web applications to be a challenge. Fortunately, Cloudinary offers an easy solution. In this article, we will […]

Read More

How to Create a React Search Bar for Your Website

How to Create a React Search Bar for Your Website

Mar 18, 2023

·

6 Min Read

A search bar is an essential component of any website, allowing visitors to find the information they need quickly. If you are building a website using React, you can easily create a search bar to improve the user experience. In this article, we will guide you through the process of creating a React search bar […]

Read More

React Hook useEffect Has a Missing Dependency: How to Fix It

React Hook useEffect Has a Missing Dependency: How to Fix It

Mar 10, 2023

·

6 Min Read

React is one of the most popular JavaScript libraries used for building user interfaces. One of the main advantages of React is the use of hooks, which allow you to use state and other React features in functional components. One of the most commonly used hooks is useEffect, which is used for handling side effects […]

Read More

Do you want more articles on React, Next.js, Tailwind CSS, and JavaScript?

Subscribe to my newsletter to receive articles straight in your inbox.

If you like my work and want to support me, consider buying me a coffee.

Buy Me A Coffee

Contact Me ☎️

Discuss A Project Or Just Want To Say Hi?
My Inbox Is Open For All.

Mail : contact@mridul.tech

Connect with me on Social Media

Contact Art