React Native: Getting the position of an element

前端 未结 6 563
太阳男子
太阳男子 2020-11-28 03:08

I am styling an Image component with flexbox to be in the center of the screen which works pretty well. Now I want a second Image component to be d

6条回答
  •  甜味超标
    2020-11-28 03:40

    I needed to find the position of an element inside a ListView and used this snippet that works kind of like .offset:

    const UIManager = require('NativeModules').UIManager;
    const handle = React.findNodeHandle(this.refs.myElement);
    UIManager.measureLayoutRelativeToParent(
      handle, 
      (e) => {console.error(e)}, 
      (x, y, w, h) => {
        console.log('offset', x, y, w, h);
      });
    

    This assumes I had a ref='myElement' on my component.

提交回复
热议问题