React Native - 如何测量 ScrollView 中内容的大小?

React Native - How to measure size of content in ScrollView?

我想测量我 ScrollView 中内容的大小。

因此,我正在使用 NativeMethodsMixin 中的 measure

import NativeMethodsMixin from 'NativeMethodsMixin'

我不太确定从这里到哪里去,与此问题相关的大多数 SO 帖子似乎都已过时。

我知道我需要为我的 ScrollView 创建一个 ref(我这样做并调用了 _scrollView,我可以使用 this.refs._scrollView 访问它)。

问题是,我不能像这样将 this.refs._scrollView 传入 measure

NativeMethodsMixin.measure(this.refs._scrollView, (data) => {
  console.log('measure: ', data)
})

然后我收到以下错误:

ExceptionsManager.js:61 findNodeHandle(...): Argument is not a component (type: object, keys: measure,measureInWindow,measureLayout,setNativeProps,focus,blur,componentWillMount,componentWillReceiveProps)

然后我尝试使用 findNodeHandle 检索实际的节点句柄并将其传递到 measure 中,如 github issue:

中所讨论
const handle = React.findNodeHandle(this.refs._scrollView)
NativeMethodsMixin.measure(handle, (data) => {
  console.log('measure: ', data)
})

但这会导致同样的错误。有什么想法吗?

ScrollView 定义了一个名为 onContentSizeChange:

的 prop
<ScrollView
  onContentSizeChange={(width, height) => {
    console.log(width, height);
  }}>
  {content}
</ScrollView>
NativeMethodsMixin.measure.call(elementToMeasure, (x, y, width, height) => {
  ...
});