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.

is it possible to get a ref to the created component?

See original GitHub issue

Hi, if I specify component="div" is it possible to get a ref to that div? The current ref returned is the React Class Component, not the div…not sure why? Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
Aarbelcommented, Nov 30, 2019

Ok solved, that works with it :

class TestTappable extends React.Component {
    divTapped = () => {
        const tappableDOMElement = ReactDOM.findDOMNode(tappableRef)
        const divPosition = tappableDOMElement.getBoundingClientRect();
    }

    render() {
        return (
            <Tappable
                type="div"
                ref={element => thisTappableElement = element}
                onTap={this.divTapped}
            >
                TEST
            </Tappable>
        )
    }
}
0reactions
Aarbelcommented, Nov 30, 2019

Thanks a lot @mattcolman ! Doesn’t work for me, it doesn’t return the DOM element but the React Class of Tappable as ref, here’s my code :

class TestTappable extends React.Component {
    divTapped = () => {
        // Returns error "getBoundingClientRect() not defined" BECAUSE this.TappableElement is not the <div> element generated by <Tappable> but the javascript React Class
        const divPosition = this.TappableElement.getBoundingClientRect();
    }

    render() {
        return (
            <Tappable
                type="div"
                ref={element => thisTappableElement = element}
                onTap={this.divTapped}
            >
                TEST
            </Tappable>
        )
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use React createRef - LogRocket Blog
Learn how to use the React.createRef() method and useRef Hook in React to simplify creating refs and interact with the HTML DOM.
Read more >
How can you create Refs after the component has mounted in ...
But now my question is how do I create the refs ? My declaration for React.createRef() is in the Constructor(){} for the first...
Read more >
Refs to Components | React
React supports a special attribute that you can attach to any component. The ref attribute can be a callback function, and this callback...
Read more >
All about React's createRef method - OpenReplay Blog
“Ref” is short for reference, and it's a React feature that allows us to store a component or DOM element of our application...
Read more >
Use React ref to Get a Reference to Specific Components
When you are using React components you need to be able to access specific references to individual component instances.
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