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.

[v4] | [v2] Typescript ref error when creating ref using createRef

See original GitHub issue

Bug

Environment info

Library Version
@gorhom/bottom-sheet 4.x.x
react-native 0.65
react-native-reanimated 2.2.x
react-native-gesture-handler 1.10.x

Steps To Reproduce

  1. create a variable outside component using createRef
  2. pass bottomsheet ref as the created variable
  3. typescript shows the error “Type ‘RefObject<Ref<BottomSheetMethods> | undefined>’ is not assignable to type ‘Ref<BottomSheetMethods> | undefined’”
  4. try to put createRef<RefObject<Ref<BottomSheetMethods> | undefined> and you cannot find BottomSheetMethods exported from the libraray

Expected Behaviour

  1. Library should export BottomSheetMethods to create a ref to be able use it as a reusable component
  2. Also closing the modal from different component uses createRef.

Reproducible sample code

export const bottomSheetRef = createRef<React.Ref<BottomSheetMethods> | undefined>();

<BottomSheet
	ref={bottomSheetRef}
	index={-1}
	snapPoints={snapPoints}
	enablePanDownToClose
>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
gchiacchiocommented, Feb 4, 2022

Issue is still happening… please reopen

0reactions
adibkadircommented, Aug 19, 2022

I’m getting this error Error: Function components cannot have string refs. We recommend using useRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref

When trying to run the app due to this. Is anybody using this component on expo react native app?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use refs in React with Typescript - Stack Overflow
If you're using React 16.3+, the suggested way to create refs is using React.createRef() . class TestApp extends React.
Read more >
Using React Refs in Typescript - Pluralsight
To use refs in a functional component, we create the ref using the useRef hook: 1const divRef = React.useRef<HTMLDivElement>(null);.
Read more >
A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >
UseRef, CreateRef, ForwardRef? What's up with refs in React?
A ref is just a reference to a value, for when you want to store some information in a component, but you don't...
Read more >
React Top-Level API
If you don't use ES6 classes, you may use the create-react-class module instead. ... createRef creates a ref that can be attached to...
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