Comments (5)
You can do something like this.
...
carousel = null;
options = {};
constructor() {
super();
let _this = this;
this.options = {
onInitialized: function(){
_this.carousel = this;
}
};
}
next() {
this.carousel.next();
}
...
from react-owl-carousel.
Hi, I have the same issue and I don't know how to solve it.
Here is my Carousel component using owl carousel
import React from 'react';
import PropTypes from 'prop-types';
import OwlCarousel from 'react-owl-carousel';
import 'owl.carousel/dist/assets/owl.carousel.css';
import 'owl.carousel/dist/assets/owl.theme.default.css';
import './CarouselRGPD.css';
const Carousel = ({ children }) => (
<OwlCarousel
className="owl-theme"
margin={15}
dots
mouseDrag={false}
touchDrag={false}
stage="25"
responsive={{
0: {
items: 1,
},
}}
>
{children}
</OwlCarousel>
);
Carousel.propTypes = {
children: PropTypes.node.isRequired,
};
export default Carousel;
Then I call it in an other component where I map on an array to create a carousel
<CarouselRGPD ref={(el) => { this.rgpd = el; }}>
{listPermission.map(perm => (
<CardContent className={classes.cardContent} key={perm.id}>
<Typography variant="headline" component="h2" className={classes.cardTitle}>
Permission: {perm.attributes.title}
</Typography>
<FormGroup>
<FormControlLabel
control={
<Switch
checked={checkedDisable}
onChange={this.handleChangeDisable('checkedDisable')}
value="checkedDisable"
color="primary"
/>
}
label="Je ne souhaite pas être contacté par une notification"
/>
</FormGroup>
....
<Button
variant="contained"
size="large"
className={classes.button}
color="primary"
onClick={e => this.handleNextClick(e)}
disabled={!navigator.onLine}
>
Suivant
</Button>
</CardContent>
))}
</CarouselRGPD>
And my handleClick
handleNextClick = (event) => {
event.preventDefault();
// get it with ref, declared as prop in Carousel
console.log(this.rgpd);
// WHAT I'M TRYING TO DO
// this.rgpd.next();
// OR
// this.rgpd.owlCarousel.trigger('next.owl.carousel');
return true;
}
So, nothing is happening because the ref rgpd doesn't have the next() method.
What it's look like in the project, so as I explained before I would like to call next() on the button's click.
Thank you for reading, I would appreciate some help with an example if possible 😃
from react-owl-carousel.
I found this confusing as well. I'm assuming you may have resolved it already! For future visitors of this issue, try this:
<OwlCarousel
onInitialize={function() {
this.next();
}}
>
Basically, if you use a regular function rather than a fat arrow function, you are in the context of the component.
from react-owl-carousel.
step 1 - In your constructor create the options object which will be passed to OwnCaroussel in step 2
constructor() { super(); this.options = { onInitialized: function(){ this.carousel = this; } };
Step 2 - Pass options to OwlCarousel as follows
<OwlCarousel {...this.options}>
Step 3 - now you can call any of carousel's methods - For an example -
this.carousel.next()
or this.carousel.prev()
etc.
Hope this helps :)
from react-owl-carousel.
The key is to use a regular function for the onInitialized event of the carousel , you can follow the steps below :
step 1 : inside the render method , define the carousel's options
const self = this; const options = { items: 4, dots: true, responsive: { 0: { items: 5, margin: 20 }, 480: { items: 4 } }, onInitialized: function () { self.carousel = this; } }
step 2 : pass the options to the carousel
<OwlCarousel {...options} > </OwlCarousel >
step 3 : in the next button , you can call the next method inside the carousel property
<button onClick={() => this.carousel.next()}>Next</button>
i hope this will work for the upcoming visitors , and help the community .
from react-owl-carousel.
Related Issues (20)
- How to filter? HOT 1
- Not able to update state variable on onChanged callback , gives error 'unstable_flushDiscreteUpdates: Cannot flush updates when React is already rendering' HOT 1
- Add Typescript support
- index.js:1 Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code.
- UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code. | Please update the following components: ReactOwlCarousel HOT 9
- window is not defined HOT 1
- SlideTransition prop doesn't exist in react
- Bank white page issue HOT 2
- autoHeightClass doesnt work
- react-owl-carousel mobile issue - one slide getting place at the top of the another slide at arr HOT 1
- i can't use methods HOT 1
- carousel installed but not working
- nav button is not working for scrolling card
- navContainer not working
- jQuery cannot see $
- dotData not working
- Blank OwlCarousel : items moved from .owl-stage to .owl-carousel when resize the window mobile
- Warning: Invalid DOM property `class`. Did you mean `className` HOT 1
- UNSAFE_componentWillReceiveProps HOT 6
- Fails to install in React 17 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-owl-carousel.