37 - Image Carousel with Autoplay

Description

Create an image carousel component that displays multiple images within a rotating carousel, automatically transitions between images at a specified interval, and provides controls for manual navigation.

Algorithm

  1. Create a React component for the image carousel

  2. Define the images array and the current image index

  3. Use the useState hook to manage the current image index

  4. Create a function to handle the autoplay functionality

  5. Create a function to handle the manual navigation controls

  6. Render the image carousel with the current image and navigation controls

Classes

  • ImageCarousel: The main image carousel component

  • ImageSlide: A single image slide component

  • NavigationControl: A navigation control component

Code

ImageCarousel.js

import React, { useState, useEffect } from 'react';
import ImageSlide from './ImageSlide';
import NavigationControl from './NavigationControl';

const ImageCarousel = () => {
  const [currentImage, setCurrentImage] = useState(0);
  const [images, setImages] = useState([
    { src: 'image1.jpg', alt: 'Image 1' },
    { src: 'image2.jpg', alt: 'Image 2' },
    { src: 'image3.jpg', alt: 'Image 3' },
  ]);

  useEffect(() => {
    const autoplay = () => {
      setCurrentImage((currentImage + 1) % images.length);
    };
    const intervalId = setInterval(autoplay, 5000);
    return () => clearInterval(intervalId);
  }, [currentImage, images]);

  const handleNavigation = (direction) => {
    setCurrentImage((currentImage + direction) % images.length);
  };

  return (
    <div>
      <ImageSlide image={images[currentImage]} />
      <NavigationControl
        onNext={() => handleNavigation(1)}
        onPrev={() => handleNavigation(-1)}
      />
    </div>
  );
};

export default ImageCarousel;

ImageSlide.js

import React from 'react';

const ImageSlide = ({ image }) => {
  return (
    <div>
      <img src={image.src} alt={image.alt} />
    </div>
  );
};

export default ImageSlide;

NavigationControl.js

import React from 'react';

const NavigationControl = ({ onNext, onPrev }) => {
  return (
    <div>
      <button onClick={onPrev}>Prev</button>
      <button onClick={onNext}>Next</button>
    </div>
  );
};

export default NavigationControl;

Explanation

The code creates an image carousel component that displays multiple images within a rotating carousel, automatically transitions between images at a specified interval, and provides controls for manual navigation. The component uses the useState hook to manage the current image index and the useEffect hook to handle the autoplay functionality. The component also defines a function to handle the manual navigation controls.

Possible Future Enhancements

  • Add support for more advanced autoplay options, such as pause on hover and autoplay delay

  • Add support for more advanced navigation controls, such as pagination and keyboard navigation

  • Add support for image captions and descriptions

  • Use a library like React Slick for more advanced image carousel functionality

Last updated