React Native - get the position of a component in a view

I am trying to get the position of a component in react native.

Let's say I have a view:

<ScrollView>
    lots of items...
    lots of items...
    lots of items...
    <View></View>
</ScrollView>

      

Eventually I want to eventually scroll the list to where the View is.

This link shows how to get the position using the native ui manager, however when run it returns:

"Attempted to measure a layout, but the offset or dimensions were NaN"

React Native: getting the position of an element

I tried

this.refs.VIEW.measure((ox, oy, width, height, px, py) => {
    //ox ... py all = 0
});

      

+3


source to share


2 answers


Sometimes, if you use a measure in componentDidMount, you have to wrap it in setTimeout:



setTimeout(() => {
    this.refs.VIEW.measure((ox, oy, width, height, px, py) => {
    });
}, 0);

      

+8


source


You might want to use the onLayout callback in the View component. setTimeout might not work 100% of the time.



<View onLayout={this.measureMyComponent} />

      

+8


source







All Articles