如何获取反应本机子组件的维度

时间:2016-11-20 11:42:15

标签: react-native

我只是想在组件内传递一个反应View,然后我想找出另一个反应组件中传递的(作为道具)视图的尺寸。 是否有可能在不使用onLayout()之前找出尺寸 渲染子组件。

 class Child extends Component {  
   render() {
    return(
     <Parent>
       <View style = {{width: 100}}>
        <View style = {{height: 50,flex:1}}>
        </View>
        <View style = {{height: 30,flex:1}}>
        </View>
       <View>
     </Parent>
    )}  
 }  
 class Parent extends Component {  
   getDimensions(child) {
     // Here I want to find the dimensions of child Component
     // In this use case it should be like height = 80 & width = 100
     // set the state height and width
    }  
   getChildComponent(child) {
     // Here I want to modify the style of Child Component
     var style = [
                   child.props.style,
                   {
                   height: this.state.height + 50,
                   width : this.state.width + 50
                  }];
      return React.cloneElement(child, {style});
    }  
   componentWillMount() {
      this.getDimensions(this.props.children);
    }  
   render() {
    return (
      <View> 
        { this.getChildComponent(this.props.children) } 
      <View>
    )}  
   }

0 个答案:

没有答案