question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem on ref in typescript

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
wibedcommented, Dec 24, 2021

you could create your own union type using:

const RefSwiper: React.FunctionComponent<
  SwiperProps & RefAttributes<SwiperCore>
> = Swiper;
2reactions
mattvb91commented, Dec 1, 2021

Just came across the same issue and my current hack is to do the following:

  const swiper = useRef() as any;
<Swiper
          onInit={(core: SwiperCore) => {
            swiper.current = core.el
          }}
/>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found