在rn中使用ref查找节点报错,
解决办法:在constructor函数中添加对this的绑定
例如
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>
);
}
网友评论