How To Use Owl Carousel In ReactJS

Introduction

 
In this article, we will learn how we can use the Owl Carousel in React application. Carousel is a slideshow for cycling a series of images or videos.
 
Prerequisites
  • Basic knowledge of React.js
  • Visual Studio Code IDE

Create ReactJS project

 
Let's first create a React application using the following command.
  1. npx create-react-app matform   
Open the newly created project in Visual Studio code and Install bootstrap in this project by using the following command.
  1. npm install --save bootstrap     
Now, open the index.js file and import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css';    

Install Owl Carousel

 
Now install owl carousel by using following command
  1. npm install react-owl-carousel --save  
Now open index.html and add jquery reference ,add following line in head 
  1. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>  
Now, right-click on the public folder. Add a new folder 'assets' and under it add a new folder and rename that to 'img' and add some demo images to this folder,
 
 
Now go to src folder and create a new component 'Owldemo1.js' and add the following reference in this component
  1. import OwlCarousel from 'react-owl-carousel';  
  2. import 'owl.carousel/dist/assets/owl.carousel.css';  
  3. import 'owl.carousel/dist/assets/owl.theme.default.css';  
Add the following code in this component.
  1. import React,{Component} from 'react';  
  2. import OwlCarousel from 'react-owl-carousel';  
  3. import 'owl.carousel/dist/assets/owl.carousel.css';  
  4. import 'owl.carousel/dist/assets/owl.theme.default.css';  
  5. import './owl.css';  
  6. export class Owldemo1 extends Component {  
  7.         render()  
  8.         {  
  9.           return (  
  10.             <div>  
  11.           <div class='container-fluid' >      
  12.            <div className="row title" style={{marginBottom: "20px"}} >      
  13.            <div class="col-sm-12 btn btn-info">      
  14.            Owl Carousel In React Application   
  15.            </div>      
  16.            </div>  
  17.        </div>  
  18.        <div class='container-fluid' >            
  19.         <OwlCarousel items={3}  
  20.           className="owl-theme"  
  21.           loop  
  22.           nav  
  23.           margin={8} >  
  24.            <div ><img  className="img" src= {'assets/img/img1.jpg'}/></div>  
  25.            <div><img  className="img" src= {'assets/img/img2.jpg'}/></div>  
  26.            <div><img  className="img" src= {'assets/img/img4.jpg'}/></div>  
  27.            <div><img  className="img" src= {'assets/img/img3.jpg'}/></div>  
  28.            <div><img className="img" src= {'assets/img/img5.jpg'}/></div>  
  29.            <div><img className="img" src= {'assets/img/img6.jpg'}/></div>  
  30.            <div><img className="img" src= {'assets/img/img7.jpg'}/></div>  
  31.       </OwlCarousel>  
  32.       </div>  
  33.   
  34.       </div>  
  35.           )  
  36.         }  
  37.       }  
  38.         
  39.   
  40. export default Owldemo1  
Now create a css file named owl.css and add following css.
  1. .title  
  2. {  
  3.         margin-bottom: 20px;  
  4.         padding:20px  
  5. }  
  6. .img  
  7. {  
  8.         height: 260px;width:100%  
  9. }  
Import  this file in owldemo1.js component.
 
Now open app.js file and add the following code.
  1. import React from 'react';  
  2. import logo from './logo.svg';  
  3. import './App.css';  
  4. import Owldemo1 from './Owldemo1'  
  5. import OwlDemo from './Owldemo'  
  6.   
  7. function App() {  
  8.   return (  
  9.     <div className="App">  
  10.       <Owldemo1/>  
  11.     </div>  
  12.   );  
  13. }  
  14.   
  15. export default App;  
Now run the project by using 'npm start' and check result,
 
Now create another component ,named 'owldemo.js' ,in this component we create auto owl  carousel.Add the following code in this component, 
  1. import React,{Component} from 'react';  
  2. import OwlCarousel from 'react-owl-carousel';  
  3. import 'owl.carousel/dist/assets/owl.carousel.css';  
  4. import 'owl.carousel/dist/assets/owl.theme.default.css';  
  5. import './owl.css';  
  6. export class OwlDemo extends Component {  
  7.         render()  
  8.         {      
  9.           return (  
  10.               <div>  
  11.             <div class='container-fluid' >      
  12.             <div className="row title" style={{marginBottom: "20px"}} >      
  13.             <div class="col-sm-12 btn btn-info">      
  14.             Owl Carousel with Auto Play Property In React Application   
  15.             </div>      
  16.             </div>  
  17.         </div>  
  18.         <div class='container-fluid' >   
  19.           <OwlCarousel items={3} margin={8} autoplay ={true} >  
  20.         <div ><img  className="img" src= {'assets/img/img1.jpg'}/></div>  
  21.            <div><img  className="img" src= {'assets/img/img2.jpg'}/></div>  
  22.            <div><img  className="img" src= {'assets/img/img4.jpg'}/></div>  
  23.            <div><img  className="img" src= {'assets/img/img3.jpg'}/></div>  
  24.            <div><img className="img" src= {'assets/img/img5.jpg'}/></div>  
  25.            <div><img className="img" src= {'assets/img/img6.jpg'}/></div>  
  26.            <div><img className="img" src= {'assets/img/img7.jpg'}/></div>  
  27.       </OwlCarousel>  
  28.       </div>  
  29.       </div>  
  30.           )  
  31.         }  
  32.       }  
  33.         
  34.   
  35. export default OwlDemo  
Now run the project and check images are auto slide.
 

Summary

 
In this article, we learned how to implement the Owl Carousel in an Reactjs application.