akiran / akiran/react-slick

slickGoTo inside useEffect doesn't work.

未关闭
#2,037 20 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
11.9k
派生
2.1k
PR 合并指标
30 天内没有已合并 PR

描述

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])
```

```

```

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。