how get parent size in child view in react-native? -
i cannot understand how can send parent view's sizes child component of this. in renderpager() want calculate parameters depend on parent view size. know can through onlayout(). problem onlayout called after building of childs (i see console log). how can it?
onpagelayout = (event) => { const { width, height } = event.nativeevent.layout; console.log("on layout"); }; render() { return ( <view style={styles.root} onlayout={this.onpagelayout} > {this.renderpager()} </view> ); } renderpager = () => { // how can root view's size here return ( <indicatorviewpager ref={(ref) => (this.viewpager = ref)} scrollenabled={!this.state.isdragging} onpagescroll={this.onpagescroll} style={styles.pageroot} > {this.renderpages()} </indicatorviewpager> ); };
thank you
you can maybe pass data props, , take prop data store. right in onlayout
triggered after rendered, means have pass props null value on first render. instance:
onpagelayout = (event) => { const { width, height } = event.nativeevent.layout; console.log("on layout"); this.setstate({width, height}) }; render() { return ( <view style={styles.root} onlayout={this.onpagelayout} > {this.renderpager(this.state.width, this.state.height)} </view> ); } renderpager = (width, height) => { // if width or height null // how can root view's size here return ( <indicatorviewpager ref={(ref) => (this.viewpager = ref)} scrollenabled={!this.state.isdragging} onpagescroll={this.onpagescroll} style={styles.pageroot} > {this.renderpages()} </indicatorviewpager> ); };
also use measure
function of parent, think might bit cumbersome.
Comments
Post a Comment