How to Create Carousel with React

Johnson Liu
3 min readJun 7, 2021

--

What is the first thing that comes to mind when you think of the word carousel? More often than not, you think about the carnival ride where you go round and round sitting on a horse. This concept fits perfectly for what we are planning to accomplish. In react when we create a carousel, the display will constantly loop as long as we are clicking on the next or previous button.

Every single web application is created with data to display or show to the viewers. You often scroll through these web pages that has lists of pictures, profiles, reviews, comments, articles, etc. There are many ways to showcase the data, and one very common way is through a carousel.

Building a carousel is actually very simple. For beginners, we can use React Bootstrap to help us implement the carousel component. First you want to import Carousel from “react-bootstrap/Carousel” in your Javascript file and then run “npm install react-bootstrap-carousel” in your code editor.

Once we have that setup, we can now use the prebuilt carousel component from React Bootstrap. If you are pulling data from an API or a database, you will most likely need to manipulate that data through a map function. Once you setup the the map function to pull the wanted data, you can set the interval if you want to the display to change in x amount of seconds (4000 = 4 seconds). You can also give it a className to provide some additional CSS editing.

Once you have that map function setup, we can now proceed to the virtual DOM where our data will be returned. In our return we want to wrap all the data or items that we want to display in our custom Carousel tag. In my example, I am wrapping a list or an array of items in the Carousel tag. If you are not pulling from an API or database, then you can hard code the data for your test.

Lastly, when the data is fetched, the carousel will be able to display the data. You can check out my end product with the link to my project Baby On Board. Have fun looping!

--

--

Johnson Liu
Johnson Liu

No responses yet