30

升级到0.26.0-rc版本后,这一行:

React.findNodeHandle(this.refs.myRef)

抛出此错误消息:

未处理的 JS 异常:_react2.default.findNodeHandle 不是函数。

我正在用这个导入 React:

import React from 'react';

文档仍然说“与往常一样,要获取组件的本机节点句柄,您可以使用 React.findNodeHandle(component)。”

4

3 回答 3

58

现在该函数可以在没有对象的情况下使用:

import {
  ...
  findNodeHandle,
  ...
} from 'react-native';

并直接调用它:

findNodeHandle(this.refs[refName])
于 2016-06-13T06:30:08.467 回答
34

您还必须导入 ReactNative。

import ReactNative from 'react-native';
...
ReactNative.findNodeHandle(...)
于 2016-05-13T06:37:11.337 回答
2
import {
  ...
  findNodeHandle,
} from 'react-native';

var RCTUIManager = require('NativeModules').UIManager;

var view = this.refs['yourRef']; // Where view is a ref obtained through <View ref='ref'/>
RCTUIManager.measure(findNodeHandle(view), (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)
})
于 2017-08-04T16:50:41.217 回答