Hi Mosh ,
Merry Christmas.
I am working on React based web application.
I am using Carousel for image slider using Script tag on index html page like below
When I navigate to different page images won’t load unless I refresh the page.
Now I am forcefully refreshing the page like this using javascript tag in index html page
Can you please give me some solution??
This is my app.
Owl Carousel uses DOM APIs to work. ReactJS is different and it abstracts the DOM APIs and helps you focus on modelling your app without worrying about the DOM APIs.
I believe that approach to use owl carousel for ReactJS is not good and I encourage to use either some library for building a slider or follow different approach (like animating the content when you click the prev / next button and updating the image with another image.)
Honestly, I haven’t used any sliders in my projects so far and this is the best answer I can provide at the moment.
However, if you insist to follow that approach, you should try the useEffect() or componentDidMount() hook to initialize the owlCarousel() object.
for class components:
---------------------
componentDidMount(){
// create ref for the div (with the id/class).
// use that ref to initialize the ownCarousel() and supply options (carousel).
}
for functional components;
---------------------------
step 1# import at the top.
import { useEffect } from 'react';
step 2#
useEffect(() => {
// steps just like above.
// reference the div and initialize the carousel object.
});
But
I can guarantee that the carousel you are using is not built for React and probably won’t work (using above technique).