akiran / akiran/react-slick

slickGoTo inside useEffect doesn't work.

Open
#2,037 20 comments 3 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
11.9k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

so I have following problem, I want my slider to go back to slide 6 after if anyone would try to swipe further.
However it doesn't work. UseEffect is being triggered, IF condition is being met, but the function won't run.
If I put the same function call inside JSX like first snippet below it works how it should:
```
sliderRef.current.slickGoTo(5)} >go to slide 6
```

This is my code:
```
const [activeSlide, setActiveSlide] = useState(0)
const sliderRef = useRef(null)
const settings = {
variableWidth: true,
speed: 500,
pauseOnHover: false,
swipeToSlide: false,
focusOnSelect: false,
infinite: false,
slidesToScroll: 1,
arrows: false,
beforeChange: (current, next) => setActiveSlide(next)
}
```

```
useEffect(() => {
if (window.innerWidth > 1100 && activeSlide > 5) {
sliderRef.current.slickGoTo(5)
}
}, [activeSlide])
```

```

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.