RN报错 TypeError: undefined is not an object (evaluating 'this.refs.demo')

在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>

    );

  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容