美文网首页
RN报错 TypeError: undefined is not

RN报错 TypeError: undefined is not

作者: fueb | 来源:发表于2020-12-21 16:39 被阅读0次

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

    相关文章

      网友评论

          本文标题:RN报错 TypeError: undefined is not

          本文链接:https://www.haomeiwen.com/subject/puarnktx.html