Horje
React-Bootstrap Carousel API

React carousels, or sliders, are a popular UI (User Interface) component used in web development to showcase multiple items or images in a single, interactive, and space-efficient container. Carousels are commonly used for displaying product images, testimonials, news articles, or any other content that can be cycled through.

Syntax:

<Carousel>
</Carousel>

We will learn about the Carousel API with the following subtopic’s:

  • Carousel
  • Carousel Item
  • Carousel Caption

Syntax:

<Carousel>
</Carousel>

Carousel has many attributes some are attributes are demonstrate below.

  • activeIndex: The index of currently active Item.
  • onSelect: A callback function when an item is selected.
  • prevIcon: Previous element of carousel element
  • nextIcon: Next element of carousel element
  • keyboard: If this ‘true’ carousel item will change by keyboard otherwise not.
  • pause: Specifies when to pause the automatic sliding. It can be “hover”, “focus”, or “false” to disable pausing.
  • direction: The direction of the slide transition. It can be either “next” or “prev”.

Syntax:

<Carousel.Item>
</Carousel.Item>

Carousel has many attributes some are attributes are demonstrate below.

  • active:This specifies whether the item is the currently active slide. When active is true, this item will be displayed as the active slide. It is string attribute.
  • className: A CSS class name to be added to the Carousel.Item element, allowing you to apply custom styles.
  • index: The index of the item in carousel.

CarouselCaption:

The carousel caption provide the caption for the carousel text.

Syntax:

<Carousel.Caption>
{Your Caption Here}
</Carousel.Caption>

Steps to create React Application And Installing Module:

Step 1: Create a React application using the following command.

npx create-react-app foldername

Step 2: After creating your project folder i.e. folder name, move to it using the following command.

cd folder_name

Step 3: After creating the ReactJS application, Install the required modules using the following command.

npm install react-bootstrap bootstrap

Step 3: Add the below line in index.js file.

import 'bootstrap/dist/css/bootstrap.css';

Project Structure:

Image

Example 1: The following program demonstrates the Carousel API.

JavaScript
/* App.js */
import React from 'react' ; 
import MyCarousel from './MyCarousel'; 

function App() { 
return ( 
    <div className='App'> 
    <MyCarousel/> 
    </div> 
) 
} 

export default App
JavaScript
import React, { useState } from 'react'; 
import Carousel from 'react-bootstrap/Carousel'; 

function DynamicCarousel() { 
const [index, setIndex] = useState(0); 

const items = [ 
    { 
    src: 'https://media.geeksforgeeks.org/wp-content/uploads/20210831104937/img1.png', 
    alt: 'First slide', 
    title: 'First Slide', 
    description: 'This is the first slide description.', 
    }, 
    { 
    src: 'https://media.geeksforgeeks.org/wp-content/uploads/20210831105013/img2-300x200.png', 
    alt: 'Second slide', 
    title: 'Second Slide', 
    description: 'This is the second slide description.', 
    }, 
    { 
    src: 'https://media.geeksforgeeks.org/wp-content/uploads/20210831105054/img3-300x200.png', 
    alt: 'Third slide', 
    title: 'Third Slide', 
    description: 'This is the third slide description.', 
    }, 
]; 

const handleSelect = (selectedIndex) => { 
    setIndex(selectedIndex); 
}; 

return ( 
    <Carousel activeIndex={index} onSelect={handleSelect}> 
    {items.map((item, i) => ( 
        <Carousel.Item key={i}> 
        <img className="d-block w-100" src={item.src} alt={item.alt} /> 
        <Carousel.Caption> 
            <h3>{item.title}</h3> 
            <p>{item.description}</p> 
        </Carousel.Caption> 
        </Carousel.Item> 
    ))} 
    </Carousel> 
); 
} 

export default DynamicCarousel; 

Steps to run the application: Run the application from the root directory of the project, using the following command.

npm start

Output:

Image-(3)

Example 2: The following program demonstrates the Carousel API with Carousel.Item using interval prop.

JavaScript
/* App.js */
import React from 'react' ;
import MyCarousel from './MyCarousel';

function App() {
  return (
    &lt;div className='App'&gt;
       &lt;MyCarousel/&gt;
    &lt;/div&gt;
  )
}

export default App
JavaScript
/* MyCarousel.js */
import React from 'react';
import { Carousel } from 'react-bootstrap';


function MyCarousel() {
  return (
    &lt;div className='bg-black text-center' style={{height:&quot;100vh&quot;}}&gt;
          &lt;Carousel&gt;
      &lt;Carousel.Item interval={1000}&gt;
        &lt;img   
          src=&quot;https://media.geeksforgeeks.org/wp-content/uploads/20230407154213/gfg-bag.jpg&quot;
          alt=&quot;First slide&quot;
          className='h'
          style={{height:&quot;450px&quot;,marginTop:&quot;30px&quot;}}
        /&gt;
        &lt;Carousel.Caption&gt;
          &lt;h3&gt;First Slide&lt;/h3&gt;
          &lt;p&gt;Some description for the first slide.&lt;/p&gt;
        &lt;/Carousel.Caption&gt;
      &lt;/Carousel.Item&gt;
      &lt;Carousel.Item interval={500}&gt;
        &lt;img
        
          src=&quot;https://media.geeksforgeeks.org/wp-content/uploads/20230407153931/gfg-tshirts.jpg&quot;
          alt=&quot;Second slide&quot;
          style={{height:&quot;450px&quot;,marginTop:&quot;30px&quot;}}
        /&gt;
        &lt;Carousel.Caption&gt;
          &lt;h3&gt;Second Slide&lt;/h3&gt;
          &lt;p&gt;Some description for the second slide.&lt;/p&gt;
        &lt;/Carousel.Caption&gt;
      &lt;/Carousel.Item&gt;
      &lt;Carousel.Item interval={200}&gt;
        &lt;img
   
          src=&quot;https://media.geeksforgeeks.org/wp-content/uploads/20230407153938/gfg-hoodie.jpg&quot;
          alt=&quot;Third slide&quot;
          style={{height:&quot;450px&quot;,marginTop:&quot;30px&quot;}}
        /&gt;
        &lt;Carousel.Caption&gt;
          &lt;h3&gt;Third Slide&lt;/h3&gt;
          &lt;p&gt;Some description for the third slide.&lt;/p&gt;
        &lt;/Carousel.Caption&gt;
      &lt;/Carousel.Item&gt;
    &lt;/Carousel&gt;

    &lt;/div&gt;
   
  );
}

export default MyCarousel;

Steps to run the application: Run the application from the root directory of the project, using the following command.

npm start

Output:

Image-(3)



Reffered: https://www.geeksforgeeks.org


ReactJS

Related
Mastering React Routing: Learn Navigation and Routing in React Apps Mastering React Routing: Learn Navigation and Routing in React Apps
React-Bootstrap Dropdown items React-Bootstrap Dropdown items
How to Host ReactJS Websites on Vercel with Environment Variables ? How to Host ReactJS Websites on Vercel with Environment Variables ?
How to migrate from create-react-app to Vite? How to migrate from create-react-app to Vite?
How to Insert an Iframe in React ? How to Insert an Iframe in React ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11