Problem on ref in typescript
See original GitHub issueCheck 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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:9 (1 by maintainers)
Top Results From Across the Web
why is typescript complaining when trying to use ref in react?
Try this: const ref = useRef() as RefObject<HTMLDivElement>; const foo = () => { if (!ref.current) return; const rect = ref.current.
Read more >React Refs with TypeScript. All your questions related to…
Refs provide a way to access DOM nodes or React elements created in the render method. Let's create some React refs with TypeScript...
Read more >useRef() Typescript problem : r/reactjs - Reddit
When your component renders for the first time, the "previous" value doesn't exist yet - it's undefined . So the return type of...
Read more >Using React Refs in Typescript - Pluralsight
It simply tests that the ref has been set - the Typescript compiler will report an error if the if statement is omitted...
Read more >Useful Patterns by Use Case - React TypeScript Cheatsheets
Forwarding Refs: As the React docs themselves note, most usecases will ... <T,> https://github.com/microsoft/TypeScript/issues/15713#issuecomment-499474386
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
you could create your own union type using:
Just came across the same issue and my current hack is to do the following: