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:45

    If you use function components and don't want to use a forwardRef to measure your component's absolute layout, you can get a reference to it from the LayoutChangeEvent in the onLayout callback.

    This way, you can get the absolute position of the element:

     {
        event.target.measure(
          (x, y, width, height, pageX, pageX) => {
            doSomethingWithAbsolutePosition({
              x: x + pageX, 
              y: y + pageY,
            });
          },
        );
      }}
    />
    

    Tested with React Native 0.63.3.

提交回复
热议问题