Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  88] [ 5]  / answers: 1 / hits: 10658  / 4 Years ago, tue, november 10, 2020, 12:00:00

I have the following React app with a Slick carousel. I created a custom Next button and need to execute the slickNext() method to proceed to the next slide. The Slick carousel docs and some of the answers for questions have mentioned the following way to call slickNext() method.


The problem is I'm getting the error slider.slick is not a function.


This is what I have tried so far


gotoNext = () => {
var slider = document.getElementsByClassName("sliderMain")[0];
console.log("set", slider)
slider.slick("slickNext");
}

const SampleNextArrow = (props) => {
const { className, style } = props;
return (
<div
className={className}
onClick={this.gotoNext}
/>
);
}

const settings = {
dots: true,
infinite: false,
speed: 500,
slidesToShow: 1,
slidesToScroll: 1,
swipe: false,
nextArrow: <SampleNextArrow className="customSlickNext" />,
};

<Slider
id="sliderMain"
{...settings}
className="sliderMain"
afterChange={this.changeSlide}
>{ /* slider goes here */}
</Slider>

How can I solve this?


More From » reactjs

 Answers
3

With react-slick, you can add a ref prop on <Slider>. If you're using hooks, you can do that with useRef():


import React, { useRef } from 'react';

...

const sliderRef = useRef();

<Slider
ref={sliderRef}
...
>
</Slider>

You can then use sliderRef to call Slick methods in your function:


gotoNext = () => {
sliderRef.current.slickNext();
}

The available methods for react-slick can be found at: https://react-slick.neostack.com/docs/api/#methods


[#2332] Thursday, November 5, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
Wed, Oct 14, 20, 00:00, 4 Years ago
Tue, Apr 14, 20, 00:00, 4 Years ago
Thu, Mar 19, 20, 00:00, 4 Years ago
;