Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Getting coordinates of a touch in React Native (Expo)

$
0
0

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?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>