JedWatson / react-tappable

Tappable component for React
http://jedwatson.github.io/react-tappable/
MIT License
862 stars 90 forks source link

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

Closed mattcolman closed 7 years ago

mattcolman commented 7 years ago

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!

mattcolman commented 7 years ago

nevermind, just realised I can use findDOMNode to get the ref.

Aarbel commented 4 years ago

@mattcolman could you explain better how you managed this problem ?

Cf #132

Thanks a lot

mattcolman commented 4 years ago

yeah so the ref from Tappable returns the Tappable instance, say tappableRef. Then you can call ReactDOM.findDOMNode(tappableRef) to get the div. Does that make sense?

Aarbel commented 4 years ago

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>
        )
    }
}
Aarbel commented 4 years ago

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>
        )
    }
}