我正在Image用flexbox设置组件的样式,使其位于屏幕中央,效果很好。现在,我希望第二个Image组件直接显示在第一个组件的顶部。第二个图像使用绝对定位。目前,我只是在猜测像素是否适合它,但是当然这是不准确的,并且在可维护性方面付出了太多努力。
我正在寻找与jQuery等效的React Native .offset()。有这样的事情吗?如果没有,实现这一目标的最佳方法是什么?
Answers:
React Native提供了一种.measure(...)方法,该方法接受回调并使用组件的偏移量和宽度/高度对其进行调用:
myComponent.measure( (fx, fy, width, height, px, py) => {
    console.log('Component width is: ' + width)
    console.log('Component height is: ' + height)
    console.log('X offset to frame: ' + fx)
    console.log('Y offset to frame: ' + fy)
    console.log('X offset to page: ' + px)
    console.log('Y offset to page: ' + py)
})
以下内容将呈现自定义组件后的布局:
class MyComponent extends React.Component {
    render() {
        return <View ref={view => { this.myComponent = view; }} />
    }
    componentDidMount() {
        // Print component dimensions to console
        this.myComponent.measure( (fx, fy, width, height, px, py) => {
            console.log('Component width is: ' + width)
            console.log('Component height is: ' + height)
            console.log('X offset to frame: ' + fx)
            console.log('Y offset to frame: ' + fy)
            console.log('X offset to page: ' + px)
            console.log('Y offset to page: ' + py)
        })        
    }
}
请注意,有时组件在componentDidMount()调用之前未完成渲染。如果从获得零measure(...),那么将其包装在中setTimeout应该可以解决问题,即:
setTimeout( myComponent.measure(...), 0 )
您可以用来在最早可用时获取组件onLayout的宽度,高度和相对父位置:
<View
  onLayout={event => {
    const layout = event.nativeEvent.layout;
    console.log('height:', layout.height);
    console.log('width:', layout.width);
    console.log('x:', layout.x);
    console.log('y:', layout.y);
  }}
>
与使用.measure()已接受的答案中所示的方法相比,这样做的好处是您不必费力地推迟.measure()呼叫setTimeout以确保测量值可用,但是缺点是它不会给您相对于测量值的偏移量。整个页面,只有相对于元素父级的页面。
我需要在ListView中找到元素的位置,并使用类似于以下内容的代码段.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);
  });
假设ref='myElement'我的组件上有一个。
UIManager.measureLayoutRelativeToParent一个setTimeout调用中,以防止其出错。还要注意,对于现代的React Native,您将想要做的import { UIManager, findNodeHandler } from 'react-native'而不是此处显示的要求。
                    UIManager并且findNodeHandle现在可以直接从“ react-native”中导入。即import { UIManager, findNodeHandle} from "react-native"
                    我有一个类似的问题,并通过结合上面的答案解决了
class FeedPost extends React.Component {
  constructor(props) {
    ...
    this.handleLayoutChange = this.handleLayoutChange.bind(this);
  }
handleLayoutChange() {
    this.feedPost.measure( (fx, fy, width, height, px, py) => {
      console.log('Component width is: ' + width)
      console.log('Component height is: ' + height)
      console.log('X offset to page: ' + px)
      console.log('Y offset to page: ' + py)
    })
  }
  render {
    return(
      <View onLayout={(event) => {this.handleLayoutChange(event) }} 
      ref={view => { this.feedPost = view; }} >
...
现在,我可以在日志中看到feedPost元素的位置:
08-24 11:15:36.838  3727 27838 I ReactNativeJS: Component width is: 156
08-24 11:15:36.838  3727 27838 I ReactNativeJS: Component height is: 206
08-24 11:15:36.838  3727 27838 I ReactNativeJS: X offset to page: 188
08-24 11:15:36.838  3727 27838 I ReactNativeJS: Y offset to page: 870
<View onLayout {(event) => {this.handleLayoutChange(event) }}  ref={view => { this.feedPost = view; }} > ....  该主视图是this.feedPost。
                    使用引用进行计算时,这在最新版本的React Native中似乎已经改变。
以这种方式声明引用。
  <View
    ref={(image) => {
    this._image = image
  }}>
并以此方式找到价值。
  _measure = () => {
    this._image._component.measure((width, height, px, py, fx, fy) => {
      const location = {
        fx: fx,
        fy: fy,
        px: px,
        py: py,
        width: width,
        height: height
      }
      console.log(location)
    })
  }
._component在React Native 0.51中使用的要求(当前最新版本,发布此答案前三天发布)。不知道从哪里得到的?对我来说,ref回调的第一个参数是带有.measure方法的组件。
                    Animated.View来代替View,例如。
                    
measure对我不起作用。必须通过元素句柄:const handle = findNodeHandle(this.refs.dropdown); UIManager.measure(handle, (x, y, ...) ...)