ref does not work in button
See original GitHub issueExplain what you did (Required)
My use case: I’m using react-native-web and I want to know if the button is hovered or not. So I need to reference the button component.
But I think it will be helpful to reference the button in all platform.
Expected behavior (Required)
It should forward the ref
Describe the bug (Required)
It is working perfectly in 3.30
but failed in the latest version. I checked the change log and I found the reason is the button component has been converted to functional. (https://github.com/react-native-elements/react-native-elements/commit/2f50ef068c85d51ed5f43bfb9831ddda432e409f#)
To Reproduce (Required)
const buttonRef = useRef(null)
const isHovered = useHover(buttonRef)
const isFocused = useFocus(buttonRef)
const isActive = useActive(buttonRef)
...
<Button
ref={buttonRef}
/>
Screenshots (Required)
Your Environment (Required):
software | version |
---|---|
react-native-elements | ^3.3.2 |
react-native | ^0.63.4 |
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Accessing button using ref - javascript - Stack Overflow
Button is a component (does not have ref by default) and button is a primitive element (always has ref by default). If you...
Read more >Referencing Values with Refs - React Docs
You can ask React to give you a ref by calling the useRef Hook. Like state, refs let you retain information between re-renders...
Read more >How to correct a #REF! error - Microsoft Support
The #REF! error shows when a formula refers to a cell that's not valid. This happens most often when cells that were referenced...
Read more >Focus management with Vue refs - Learn web development
Over-using tabindex="-1 " can cause problems for all sorts of users, so only use it exactly where you need to. You should also...
Read more >Everything You Need to Know About Refs in React
You can fix this by adding a safe condition ref => ref && ref.focus() or by binding the callback to a class method...
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
Hey @uwwangxiangyu can we do like this?
Then use it
Just reference the button component.
For my case, I’m using
react-native-web
so I have something like this. You don’t need to test like this, you can just test if thebuttonRef
is referencing the button components, and there is no warning likeAttempts to access this ref will fail
.