How to use bootstrap carousel in react js?

Hi Friends 👋,

Welcome To aHoisting!

To use bootstrap carousel you have to import carousel from react-bootstrap that’s how you can use bootstrap carousel in react js.

Today, I am going to show you, how to use bootstrap carousel in react js.

Installation

Install the following packages to use react-bootstrap in react js.

npm

npm install react-bootstrap bootstrap

yarn

yarn add react-bootstrap bootstrap

Table of contents

  • Install and create a new React app.
  • Import react component.
  • Create a Component.

Let’s start with the first step.

Step 1: Install and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import react component.

After installing, you have to import your React component.

import React, {useState} from "react"; 
import Carousel from 'react-bootstrap/Carousel';
import 'bootstrap/dist/css/bootstrap.min.css';

Step 3: Create a Component.

You can use <Carousel activeIndex={index} onSelect={handleSelect}> to use bootstrap carousel in react js.

<Carousel activeIndex={index} onSelect={handleSelect}>
       {data.map((slide, i) => {
        return (
          <Carousel.Item>        
        <img
          className="d-block w-100 h-80"
          src={slide.image}
          alt="slider image"
          style={{height: '500px'}}
        
        />
        <Carousel.Caption>
          <h3>{slide.caption}</h3>
          <p>{slide.description}</p>
        </Carousel.Caption>
      </Carousel.Item>
      )
      })}
      
</Carousel>

Use bootstrap carousel example.

The below code is an example of a React. You have to import carousel from react-bootstrap that’s how you can use bootstrap carousel in react js.

App.js

import React, {useState} from "react"; 
import Carousel from 'react-bootstrap/Carousel';
import 'bootstrap/dist/css/bootstrap.min.css';

const data = [
  {
   image: require('./architecture.jpg'), 
   caption:"Caption",
   description:"Description Here"
  },
  {
    image:require('./beautiful-nature.jpeg'), 
    caption:"Caption",
    description:"Description Here"
   },
   {
    image:require('./bird-92.jpg'), 
    caption:"Caption",
    description:"Description Here"
   } 
]

function HomeCarousel() {
  const [index, setIndex] = useState(0);
  const handleSelect = (selectedIndex, e) => {
    setIndex(selectedIndex);
  };

  return (
    <Carousel activeIndex={index} onSelect={handleSelect}>
       {data.map((slide, i) => {
        return (
          <Carousel.Item>        
        <img
          className="d-block w-100 h-80"
          src={slide.image}
          alt="slider image"
          style={{height: '500px'}}
        
        />
        <Carousel.Caption>
          <h3>{slide.caption}</h3>
          <p>{slide.description}</p>
        </Carousel.Caption>
      </Carousel.Item>
        )
      })}
      
    </Carousel>
  );
} 
export default HomeCarousel;

In the above code example, I have used the react-bootstrap to use bootstrap carousel in react js.

Check the output of the above code.

React, bootstrap, carousel

All the best 👍