在rn中使用ref查找节点报错,
解决办法:在constructor函数中添加对this的绑定
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | constructor(props) { super(props); // 这边绑定是必要的,这样 `this` 才能在回调函数中使用 this.showMeasure = this.showMeasure.bind(this); } componentDidMount (){ setTimeout(this.showMeasure); //需要在页面加载完毕之后对视图进行测量,所有需要setTimeout } showMeasure (){ UIManager.measure( findNodeHandle(this.refs.demo), (x,y,w,h,top,bottom)=>{ console.log( "width:" + w); console.log( "height:" + h); console.log( "X offset to frame:" + x); //这个值无用 console.log( "Y offset to frame:" + y); //这个值无用 },) } render() { return ( <View> <HelloWord ref="demo" style={styles.mapStyle} > </HelloWord> </View> ); } |