I am working on a react native app and I want to handle touches on screen.
One use case is when the user "press" on screen, I want to be able to get the position (x,y) of a specific component on screen to know if it matches the (x,y) of the touch.
I've searched already on stack overflow, but none of the given solutions worked...
In my root component:
_onPress = () => {
// How can I get the position of my component ?
this._myComponent.xxx();
};
render() {
return (
<View><MyComponent ref={(r) => this._myComponent = r;} /></View>
);
}
EDIT: After trying this solution (React Native: Getting the position of an element) I made it work as follow:
In MyComponent.js:
getPosition () => {
this._ref._component.measure((width, height, px, py, fx, fy) => {
const location = {
fx: fx,
fy: fy,
px: px,
py: py,
width: width,
height: height
}
console.log(location)
});
};
render() {
return (
<View ref={(r) => { this._ref = r;} } />
);
}
Thanks for your help!