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.

ref does not work in button

See original GitHub issue

Explain 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
arpitBhallacommented, Apr 23, 2021

Hey @uwwangxiangyu can we do like this?

const MyButton=React.forwardRef((props,ref)=><Button
   TouchableComponent={() => (
       <Pressable ref={ref} {...props} />
     )}
/>)

Then use it

import React, { useRef } from 'react'
import { useActive, useFocus, useHover } from 'react-native-web-hooks'

const buttonRef = useRef(null)
const isHovered = useHover(buttonRef)
const isFocused = useFocus(buttonRef)
const isActive = useActive(buttonRef)

<MyButton
   ref={buttonRef}
   buttonStyle={backgroundColor: isHovered ? '#58b1ba' : '#4fa9b3'}
/>
1reaction
uwwangxiangyucommented, Apr 15, 2021

@uwwangxiangyu Can you please provide your code, so I can test 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 the buttonRef is referencing the button components, and there is no warning like Attempts to access this ref will fail.

import React, { useRef } from 'react'
import { useActive, useFocus, useHover } from 'react-native-web-hooks'

const buttonRef = useRef(null)
const isHovered = useHover(buttonRef)
const isFocused = useFocus(buttonRef)
const isActive = useActive(buttonRef)

<Button
    ref={buttonRef}
/>
Read more comments on GitHub >

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

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