-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[React] Typescript errors in ref
and onSwiper
props
#5500
Comments
hey, this is not a problem with Swiper or Typescript, i would say this is not a problem at all
to do that you should write import type { Swiper } from 'swiper';
const [swiperInstance, setSwiperInstance] = useState<Swiper | null>(null); |
Do you want access swiper container?? function App() {
const ref = useRef<SwiperCore>();
<Swiper
onInit={(core: SwiperCore) => {
ref.current = swiper.el;
// "swiper.el" is what you want
}}
></Swiper>
} |
@rtabulov The
Thinking out loud, you should consider adding last name Tends to be confusing without using |
@youngkyo0504 Taking that same code to my example in codesandbox how would it be? Using |
//@ts-ignore |
this is also can be avoided by - its Swiper 6.8 |
This is what I had to do with Swiper and React import { Swiper, SwiperSlide } from 'swiper/react'
import { EffectCoverflow, type Swiper as SwiperRef } from 'swiper' Then const swiperRef = useRef<SwiperRef>() And onSwiper={(swiper) => {
swiperRef.current = swiper
}} Why there is a component called Swiper and a Interface called Swiper is beyond me! Really |
That's another "issue" that types have the same name as components and one must end up adding aliases to fix the name clash. All component types should be renamed to |
This is correct usage from 9.3.2 import { useEffect, useRef, useState } from "react";
import { Swiper, SwiperRef, SwiperClass } from "swiper/react";
import "swiper/css";
export const index = () => {
const [instance, setInstance] = useState<SwiperClass | null>(null);
const swiperElRef = useRef<SwiperRef>(null);
useEffect(() => {
instance?.slideTo(2)
// ref usage
console.log(swiperElRef.current?.swiper.activeIndex);
}, [])
return <Swiper ref={swiperElRef} onSwiper={setInstance}>This is a test page</Swiper>;
};
export { index as default }; |
Check that this is really a bug
Reproduction link
https://codesandbox.io/s/swiper-typescript-types-errors-437w68
Bug description
Using Swiper with Typescript it throws errors in the
ref
andonSwiper
props.Expected Behavior
It should not throw an error even if types are assigned to it.
Actual Behavior
No response
Swiper version
8.0.6
Platform/Target and Browser Versions
macOs bigsur Chrome lastest
Validations
Would you like to open a PR for this bug?
The text was updated successfully, but these errors were encountered: