akiran / react-slick

React carousel component
http://react-slick.neostack.com/
MIT License
11.72k stars 2.1k forks source link

How to get currentSlide Index #597

Closed anandrikka closed 7 years ago

anandrikka commented 7 years ago

How can we get the current slide index, so that I can make a http call to fetch more. Current documentation doesn't help much. Can anyone help me out ?

akiran commented 7 years ago

you can user beforeChange or afterChange methods https://github.com/akiran/react-slick/blob/master/examples/SlideChangeHooks.js

ArtemKha commented 4 years ago

you can user beforeChange or afterChange methods https://github.com/akiran/react-slick/blob/master/examples/SlideChangeHooks.js

I need such api for testing and these methods are not so convenient

dominicg666 commented 3 years ago

how to get the currently active item,

When I have tried to add focusOnSelect event, but not getting the properly current index of an item, please help me, anyone tried this?

Lloydinator commented 2 years ago

In case anyone struggles with this in the future, you could hook into beforeChange like this:

const settings = {
        arrows: false,
        dots: false,
        infinite: true,
        speed: 500,
        slidesToShow: 1,
        slidesToScroll: 1, 
        beforeChange: (oldIndex, newIndex) => {
            switch (newIndex) {
                case 0: 
                    setThing('thing1')
                    break
                case 1:
                    setThing('thing2')
                    break
                 ...
                default:
            }
        }
    }

Of course, this is an example and you could do it in any way that you wish.

max8989 commented 8 months ago

It works for me with beforeChange(), AfterChange() is never called.

const [currentSlideNumber, setCurrentSlideNumber] = useState(0);

<Slider {...settings} beforeChange={(currentSlide: number, nextSlide: number) => setCurrentSlideNumber(nextSlide)}>
Roopaish commented 7 months ago

Better use afterChange, if you are using useState to keep track of currentIndex which is being used to render dots/thumbs differnetly to avoid animation from being janky

<Slider
          ref={slider}
          afterChange={(newIndex) => {
            setCurrentIndex(newIndex)
          }}
        >
        ...
        </Slider>