DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Trending

  • LLM Integration in Enterprise Applications: A Practical Guide
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  1. DZone
  2. Coding
  3. Java
  4. How to Use Owl Carousel in ReactJS

How to Use Owl Carousel in ReactJS

In this article, see how to use Owl Carousel in ReactJS.

By 
Sanwar Ranwa user avatar
Sanwar Ranwa
DZone Core CORE ·
Feb. 25, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
48.7K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

In this article, we will learn how we can use the Owl Carousel in a 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:

JavaScript
 




xxxxxxxxxx
1


 
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:

Java
 




xxxxxxxxxx
1


 
1
npm install --save bootstrap 


    

Now, open the index.js file and import Bootstrap:

Java
 




xxxxxxxxxx
1


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



You might also be interested in: React.js Essentials

Install Owl Carousel

Now install Owl Carousel by using the following command:

Java
 




xxxxxxxxxx
1


 
1
npm install react-owl-carousel --save



Now open index.html, add jquery reference, add the following line in head 

Java
 




xxxxxxxxxx
1


 
1
  <script src="https:code.jquery.com/jquery-3.4.1min.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

Java
 




xxxxxxxxxx
1


 
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.

Java
 




xxxxxxxxxx
1
40


 
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 the following CSS:

Java
 




xxxxxxxxxx
1


 
1
.title  
2
{  
3
        margin-bottom: 20px;  
4
        padding:20px  
5
}  
6
.img  
7
{  
8
        height: 260px;width:100%  
9
}  



Import this file in the owldemo1.js component.

Open the app.js file and add the following code:

Java
 




xxxxxxxxxx
1
15


 
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></Owldemo1>  
11
    </div>  
12
  );  
13
}  
14
  
15
export default App;  



Run the project by using 'npm start' and check the result.

Now create another component named 'owldemo.js'. In this component, we create auto owl carousel. Add the following code in this component:

Java
 




xxxxxxxxxx
1
35


 
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  



Run the project and check that the images are auto slide.

Summary 

In this article, we learned how to implement the Owl Carousel in a ReactJS application.

Further Reading

Everything React: Tutorials for Beginners and Experts Alike

Java (programming language) Visual Studio Code

Opinions expressed by DZone contributors are their own.

Related

  • Setting Up a Local Development Environment With IntelliJ, DevContainers, and Amazon Linux 2023
  • Getting Started With Agentic Workflows in Java and Quarkus
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Alternative Structured Concurrency

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook