I need to get (relative) coordinates of a click/touch in React Native.
I tried using RN <View onPress={(e: GestureResponderEvent) => ...}>
On the Web, I further use e.nativeEvent
which is of the type PointerEvent
, and I use layerX, layerY
which give me exactly numbers I need.
Unfortunately, the iOS native event has similar fields named locationX
and locationY
.
I have to use this ugly code to get the coordinates
interface IEvent { layerX?: number layerY?: number locationX?: number locationY?: number}const { layerX, layerY, locationX, locationY } = e.nativeEvent as unknown as IEventconst x = layerX ?? locationXconst y = layerY ?? locationY
Is there a better cross-platform way of doing it? Perhaps I need to use another control or another event?