【问题标题】:React' findNodeHandle method stopped workingReact 的 findNodeHandle 方法停止工作
【发布时间】:2016-09-08 20:49:11
【问题描述】:

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

React.findNodeHandle(this.refs.myRef)

抛出此错误消息:

未处理的 JS 异常:_react2.default.findNodeHandle 不是 功能。

我正在用这个导入 React:

import React from 'react';

文档still say: “和往常一样,要获取组件的本地节点句柄,可以使用 React.findNodeHandle(component)。”

【问题讨论】:

  • 版本0.26-rc是什么?哪个文档这么说的?
  • 有什么方法可以显示更多代码?
  • @NaderDabit ,没什么特别的:jsfiddle.net/44fk655d
  • 您应该导入react 还是react-native?好像您正在导入react。需要明确的是,您是说这在 0.25 上运行良好,但在候选发布版本上失败了?

标签: javascript react-native ecmascript-6 react-native-android react-native-ios


【解决方案1】:

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

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

并直接调用:

findNodeHandle(this.refs[refName])

【讨论】:

    【解决方案2】:

    你也必须导入 ReactNative。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-29
        • 2017-04-05
        • 2016-09-09
        • 2013-06-20
        • 1970-01-01
        • 2021-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多