swiper: Problem on ref in typescript
Check that this is really a bug
- I confirm
Reproduction link
https://codesandbox.io/s/swiper-default-react-forked-scddn?file=/src/App.tsx
Bug description
i want to use ref in typescript but i got this error on IDE and build time
this is my code:
const swiperRef = useRef() as React.RefObject<any> <Swiper ref={swiperRef} className="mySwiper">
this is error message:
Type '{ children: Element[]; ref: RefObject<any>; className: string; }' is not assignable to type 'IntrinsicAttributes & SwiperProps & { children?: ReactNode; }'. Property 'ref' does not exist on type 'IntrinsicAttributes & SwiperProps & { children?: ReactNode; }'
i’m not sure i’m doing something wrong or this is a bug.
Expected Behavior
No response
Actual Behavior
No response
Swiper version
7.3.1
Platform/Target and Browser Versions
chrome
Validations
- Follow our Code of Conduct
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
- Make sure this is a Swiper issue and not a framework-specific issue
Would you like to open a PR for this bug?
- I’m willing to open a PR
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 15 (1 by maintainers)
the correct type (for newer versions of Swiper) for Swiper instance is SwiperClass. So just do:
import SwiperClass from 'swiper'
const swiperRef = useRef<SwiperClass>()
you could create your own union type using:
Just came across the same issue and my current hack is to do the following:
I solved the typescript issue by using
SwiperRef
type imported fromswiper/react
and use swiper functions by accessing them fromcurrent.swiper
like this:Thanks I’m doing this right now but I think this is not the right way
Thanks this solution worked for me
Yup its for sure just a workaround hack to get the ref. I haven’t been able to find a solution for working with the ref in another way