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

Question: Disable Next and Previous Buttons outside the slider

Open berenicestr opened this issue 2 years ago • 5 comments

Hi,

I use custom previous and next buttons outside the slider.

https://react-slick.neostack.com/docs/example/previous-next-methods

I have the props: infinite: false, How can I disable the next and previous when is the first and the last slide?

Thanks in advance.

This is my code with the latest version of react and react-slick:

import { useState } from 'react';
import Slider from "react-slick”;

var settings = {
  dots: true,
  infinite: false,
  speed: 500,
  slidesToShow: 4,
  slidesToScroll: 1,
  initialSlide: 0,
  responsive: [
    {
      breakpoint: 1024,
      settings: {
        slidesToShow: 3,
        slidesToScroll: 1,
        infinite: true,
        dots: true
      }
    },
    {
      breakpoint: 600,
      settings: {
        slidesToShow: 2,
        slidesToScroll: 1,
        initialSlide: 2
      }
    },
    {
      breakpoint: 480,
      settings: {
        slidesToShow: 1,
        slidesToScroll: 1
      }
    }
  ]
}; 

const [slider, setSlider] = useState<any>(null);
 
  const next = () => {
    slider?.slickNext();
  };
  const previous = () => {
    slider?.slickPrev();
  };

 <div style={{ textAlign: "center" }}>
      <button onClick={previous}>Previous</button>
      <button onClick={next}>Next</button>
        </div>
        <div className="slider-container">
      <Slider ref={(c: any) => setSlider(c)} {...settings}>
          <div>item1</div>
          <div>item2</div>
          <div>item3</div>
      </slider>
</div>

berenicestr avatar Feb 06 '24 11:02 berenicestr

when you use "infinite: false" it resolved the issue or not?

Lala-kumar avatar Feb 07 '24 05:02 Lala-kumar

@Lala-kumar Your comments are not helpful.

This project needs in-depth understanding of the library to contribute. Please don't make comments on the issues of this library.

akiran avatar Feb 07 '24 05:02 akiran

Thanks @Lala-kumar It doesn't solve my problem. It might be better to disable the buttons, but I don't know how.

Please any help? Thanks in advance

berenicestr avatar Apr 24 '24 11:04 berenicestr

Please, Any help or advance @akiran ? I don’t know if there is a function in the library.

Thanks in advance

berenicestr avatar Jul 12 '24 14:07 berenicestr

I have found a solution with useState and setActiveSlide beforeOnChange. Link doc: https://react-slick.neostack.com/docs/example/slide-change-hooks

const [activeSlideTopNotifications, setActiveSlideTopNotifications] = React.useState(0);
  var settingsTopNotifications = {
    dots: true,
    infinite: false,
    speed: 500,
    slidesToShow: 1,
    slidesToScroll: 1,
    beforeChange: (current: any, next: any) => {
      setActiveSlideTopNotifications(next);
    },
  };

<div style={{ textAlign: "right", marginRight: 0 }}>
                        <IconButton aria-label="help" color="primary" onClick={previous} disabled={activeSlideTopNotifications == 0 ? true : false } >
                            <ArrowLeftIcon />
                        </IconButton>
                        <IconButton aria-label="help" color="primary" onClick={next} disabled={activeSlideTopNotifications == 5 ? true : false }  >
                            <ArrowRightIcon />
                        </IconButton>
                    </div>
            </div>

berenicestr avatar Jul 18 '24 16:07 berenicestr