react-slick icon indicating copy to clipboard operation
react-slick copied to clipboard

getting current slide index

Open csmichaelellis opened this issue 9 years ago • 4 comments

i was wondering if there is any way to get the current slide index?

csmichaelellis avatar Feb 08 '17 23:02 csmichaelellis

Try this solutions: on component create method for example

nextClick: function(e){ //alert('next'); console.log(e); }

on render function I added like this render: function() { .....

var settings = { dots: false, infinite: true, speed: 500, swipeToSlide: false, arrows: true, slidesToShow: 2, afterChange: this.nextClick, slidesToScroll: 2 };

return (<Slider {...settings}/>) }

And I got on console 2 4 6 8 10 (Because I show 2 slider Item in one time And you can keep from "e" variable current Slider Item

I hope it's help you

flyupstudio avatar Mar 22 '17 09:03 flyupstudio

use afterChange method, it allows to get currentSlide index as callback function's parameter

afterChangeHandler(currentSlide) {
        this.props.updateInitialSlide(currentSlide);
}

<Slider {...settings} afterChange={this.afterChangeHandler}>

VladPoe avatar Mar 12 '18 14:03 VladPoe

Example saving the current index into currentIndex state:

 const [currentIndex, setCurrentIndex] = useState(0);
 
  <SlickSlider
        beforeChange={(currentSlide, nextSlide) => {
              setCurrentIndex(nextSlide);
          }}
    >

Anna-kozianowska avatar Oct 07 '22 08:10 Anna-kozianowska

Example saving the current index into currentIndex state:

 const [currentIndex, setCurrentIndex] = useState(0);
 
  <SlickSlider
        beforeChange={(currentSlide, nextSlide) => {
              setCurrentIndex(nextSlide);
          }}
    >

This only works if slidesToShow is 1. I can't figure out how to make it work with more then one slide per page.

1lander avatar May 17 '23 14:05 1lander