[v4] | [v2] Typescript ref error when creating ref using createRef
See original GitHub issueBug
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
- create a variable outside component using createRef
- pass bottomsheet ref as the created variable
- typescript shows the error “Type ‘RefObject<Ref<BottomSheetMethods> | undefined>’ is not assignable to type ‘Ref<BottomSheetMethods> | undefined’”
- try to put createRef<RefObject<Ref<BottomSheetMethods> | undefined> and you cannot find BottomSheetMethods exported from the libraray
Expected Behaviour
- Library should export BottomSheetMethods to create a ref to be able use it as a reusable component
- 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:
- Created 2 years ago
- Comments:5
Top 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 >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 FreeTop 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
Top GitHub Comments
Issue is still happening… please reopen
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?