Skip to content

Instantly share code, notes, and snippets.

@charlesbodman
Last active April 25, 2018 17:13
Show Gist options
  • Select an option

  • Save charlesbodman/88a58f67fb12fa0ff81acc59322ff15b to your computer and use it in GitHub Desktop.

Select an option

Save charlesbodman/88a58f67fb12fa0ff81acc59322ff15b to your computer and use it in GitHub Desktop.
AutoSlide component of the Nuka Carousel
import React, {Component} from 'react';
import Carousel from 'nuka-carousel';
import shallowCompare from 'react-addons-shallow-compare';
class AutoCarousel extends Component {
constructor()
{
super();
this.autoPlayTimeout = false;
}
componentWillUnmount() {
this.stopAutoSlide();
}
componentDidMount() {
this.autoSlide();
}
shouldComponentUpdate(nextProps, nextState) {
return shallowCompare(this, nextProps, nextState);
}
stopAutoSlide()
{
clearTimeout(this.autoPlayTimeout);
}
autoSlide()
{
if(this.props.autoTime)
{
var carousel = this.carousel;
var carouselState = carousel.state;
var nextSlide = carouselState.currentSlide + 1;
if(nextSlide >= carouselState.slideCount)
{
nextSlide = 0;
}
this.stopAutoSlide();
this.autoPlayTimeout = setTimeout(carousel.goToSlide.bind(null,nextSlide),this.props.autoTime);
}
}
afterSlide()
{
this.autoSlide();
}
render()
{
return (<Carousel ref={(c)=>this.carousel = c} {...this.props } afterSlide={this.afterSlide.bind(this)} >
{this.props.children}
</Carousel>);
}
}
module.exports = AutoCarousel;
@charlesbodman
Copy link
Author

Usage

    <AutoCarousel slidesToShow={1}  dragging={true} autoTime={1000} >
            //Children                  
    </AutoCarousel>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment