React' findNodeHandle method stopped working

江枫思渺然 提交于 2019-12-09 14:21:49

问题


After upgrade to 0.26.0-rc version, this line:

React.findNodeHandle(this.refs.myRef)

Throws this error message:

Unhandled JS Exception: _react2.default.findNodeHandle is not a function.

I'm importing React with this:

import React from 'react';

Docs still say: "As always, to obtain a native node handle for a component, you can use React.findNodeHandle(component)."


回答1:


Now the function may be used without object:

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

And call it directly:

findNodeHandle(this.refs[refName])



回答2:


You have to import ReactNative as well.

import ReactNative from 'react-native';
...
ReactNative.findNodeHandle(...)



回答3:


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)
})


来源:https://stackoverflow.com/questions/37196734/react-findnodehandle-method-stopped-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!