1

如果只有ref,您如何測量react-native元素的高度?Reaction-native ref的測量高度(ListView)

我看到NativeMethodsMixin但不清楚如何實現它。

我試過,包括它:

import { NativeMethodsMixin } from 'react-native'; 
class Foo extends Component { 
    mixins: [NativeMethodsMixin] 
    measure(ref) { 
    console.log(NativeMethodsMixin.measure(ref)); 
    } 
    ... 
} 

,但應用程序抱怨NativeMethodsMixin是不確定的。

如果我去了RCTUIManager路線:

import { NativeModules } from 'react-native'; 
const RCTUIManager = NativeModules.UIManager; 
class Foo extends Component { 
    measure(ref) { 
    const height = RCTUIManager.measure(ref, (x, y, width, height, pageX, pageY) => height)); 
    console.log(height); 
    } 
    ... 
} 

然後我得到一個錯誤:Uncaught TypeError: Converting circular structure to JSON

我只是想拿到ref的高度。我錯過了什麼?

+0

對於任何找到這一點 - 你必須使用一個'ref',而不是實際的反應支持類實例。 https://facebook.github.io/react/docs/more-about-refs.html – chandlervdw

回答

0

你可以這樣做:

this.refs[your ref key].measure((fx, fy, width, height, px, py) => { 
      this.state.origins.push({ 
      x: px, 
      y: py, 
      width, 
      height, 
      }); 
     }); 

這個鏈接也許有用:click here