,

How to Create a Carousel Using React Bootstrap: Step-by-Step Guide with Source Code (2023)

Posted by

React Bootstrap Carousel

const { Carousel, CarouselItem, CarouselControl, CarouselIndicators, CarouselCaption } = Reactstrap;

const items = [
{
src: ‘https://via.placeholder.com/800×400’,
altText: ‘Slide 1’,
caption: ‘Slide 1’
},
{
src: ‘https://via.placeholder.com/800×400’,
altText: ‘Slide 2’,
caption: ‘Slide 2’
},
{
src: ‘https://via.placeholder.com/800×400’,
altText: ‘Slide 3’,
caption: ‘Slide 3’
}
];

class Example extends React.Component {
constructor(props) {
super(props);
this.state = { activeIndex: 0 };
this.next = this.next.bind(this);
this.previous = this.previous.bind(this);
this.goToIndex = this.goToIndex.bind(this);
}

next() {
const nextIndex = this.state.activeIndex === items.length – 1 ? 0 : this.state.activeIndex + 1;
this.setState({ activeIndex: nextIndex });
}

previous() {
const nextIndex = this.state.activeIndex === 0 ? items.length – 1 : this.state.activeIndex – 1;
this.setState({ activeIndex: nextIndex });
}

goToIndex(newIndex) {
this.setState({ activeIndex: newIndex });
}

render() {
const { activeIndex } = this.state;

const slides = items.map(item => {
return (
{}}>
{item.altText}

);
});

return (

{
`.custom-tag {
max-width: 100%;
height: 500px;
}
}`
}

{slides}

);
}
}

ReactDOM.render(, document.getElementById(‘root’));

0 0 votes
Article Rating
5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@julianadelosrios9876
11 months ago

what is the use of mock.js, i can put the link of tihe image directly without this file right?

@adrianmedina7746
11 months ago

Es lo que buscaba, gracias

@CastleBomber
11 months ago

thanks! realized its important to install "react-bootstrap" in the appropriate folder. My project has two package.json's at different levels. The root level json was not reached. needed to activate the command in my "client"/"frontend" folder

@fabianrr
11 months ago

Nice, how to change the carousel size or the images?

Cheers.

@domingomolina3246
11 months ago

Thank you very much! You helped me a lot fr!! Bless you!