Home > Mobile >  How to get offsetTop of Element compared to Screen in React Native
How to get offsetTop of Element compared to Screen in React Native

Time:12-14

I am in need of the Y coordinate, or offsetTop, of an element in React Native compared to the Screen ( NOT its Parent ).

I have tried by ref.current.offsetTop which resulted in undefined. I have tried by onLayout, which resulted that the Y is relative to its parent. I have tried via UIManager, which resulted in the application to stop working. I have tried via Measure method, which resulted in it being undefined...

CodePudding user response:

maybe you can try measure to get the values. you need pageX and pageY. also set collapsable={false}.

<View collapsable={false}
 onLayout={(event) => {
   event.target.measure((x, y, width, height, pageX, pageY) => {
    console.log(pageX)
    console.log(pageY)
 })
}}>

refer to this too https://github.com/facebook/react-native/issues/29712

  • Related