React-Native : measure a View

后端 未结 3 1174
醉酒成梦
醉酒成梦 2020-12-31 05:05

I\'m trying to measure a view using a ref, but the width returned is 0 despite the fact that I see the view displayed on the screen (and it\'s far

3条回答
  •  太阳男子
    2020-12-31 05:25

    To measure view size, no need to use ref at all. You can get it from nativeEvent passed by onLayout.

    measureView(event: Object) {
       console.log(`*** event: ${JSON.stringify(event.nativeEvent)}`);
       // you'll get something like this here:
       // {"target":1105,"layout":{"y":0,"width":256,"x":32,"height":54.5}}
    }
    
    render() {
        return (
             {this.measureView(event)}} />
        );
    }
    

提交回复
热议问题