Paw*_*icz 12 reactjs swiper.js
我在我的 React 应用程序中使用swiper.js库。我想使用 slipTo 方法形成 swiper API,但我不知道该怎么做。
我的代码
const swiperRef = useRef(null)
useEffect(() => {
//...some logic
//if() {
swiperRef.current.slideTo(0);
//}
}
return(
<Swiper pagination={true} navigation={true} ref={swiperRef}>
)
Run Code Online (Sandbox Code Playgroud)
TypeError: swiperRef.current.slideTo is not a function
Kev*_*ata 11
代替
swiperRef.current.slideTo(0);
Run Code Online (Sandbox Code Playgroud)
和
swiperRef.current.swiper.slideTo(0);
Run Code Online (Sandbox Code Playgroud)
swiperRef.current只是引用不包含js函数slideTo()的HTML元素。
<Swiper
pagination={true}
navigation={true}
onSwiper={(swiper) => setSwiper(swiper)}
>
Run Code Online (Sandbox Code Playgroud)
const [swiper, setSwiper] = useState<Swiper>();
Run Code Online (Sandbox Code Playgroud)
swiper.slideTo(0, 500); // pass speed as a second parameter
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13587 次 |
| 最近记录: |