Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 当componentdidmount安装时获取组件高度_React Native - Fatal编程技术网

React native 当componentdidmount安装时获取组件高度

React native 当componentdidmount安装时获取组件高度,react-native,React Native,我使用0.21.0 react native, 我有一个自定义组件 对于动画,我找到了获得高度的解决方案。 宽度是使用维度获取的。但身高不一样 如何获取组件的高度?正如@Chris Geirman在评论中指出的,您可以使用onLayout来实现此目的。下面是获取此构件高度的示例: var CustomComponent = React.createClass({ getInitialState: function() { return { componentHeight: 0 };

我使用0.21.0 react native, 我有一个自定义组件

对于动画,我找到了获得高度的解决方案。 宽度是使用维度获取的。但身高不一样


如何获取组件的高度?

正如@Chris Geirman在评论中指出的,您可以使用
onLayout
来实现此目的。下面是获取此构件高度的示例:

var CustomComponent = React.createClass({

  getInitialState: function() {
    return { componentHeight: 0 };
  },

  render: function() {
    return (
      <View onLayout={this._onLayoutEvent}>
        <Text>This is my Custom Component</Text>
      </View>
    );
  },

  _onLayoutEvent: function(event) {
    this.setState({componentHeight: event.nativeEvent.layout.height});
  }
});
var CustomComponent=React.createClass({
getInitialState:函数(){
返回{componentHeight:0};
},
render:function(){
返回(
这是我的自定义组件
);
},
_onLayoutEvent:函数(事件){
this.setState({componentHeight:event.nativeEvent.layout.height});
}
});

现在您可以访问组件的高度,如@Chris Geirman在评论中指出的
this.state.componentHeight
,您可以使用
onLayout
。下面是获取此构件高度的示例:

var CustomComponent = React.createClass({

  getInitialState: function() {
    return { componentHeight: 0 };
  },

  render: function() {
    return (
      <View onLayout={this._onLayoutEvent}>
        <Text>This is my Custom Component</Text>
      </View>
    );
  },

  _onLayoutEvent: function(event) {
    this.setState({componentHeight: event.nativeEvent.layout.height});
  }
});
var CustomComponent=React.createClass({
getInitialState:函数(){
返回{componentHeight:0};
},
render:function(){
返回(
这是我的自定义组件
);
},
_onLayoutEvent:函数(事件){
this.setState({componentHeight:event.nativeEvent.layout.height});
}
});

现在您可以访问组件的高度,如
this.state.componentHeight

签出
onLayout
。我们可能会更乐于使用代码查看atCheck out
onLayout
。我们可能会更有用的代码来查看hank you@Andru,我需要用动画值设置高度<代码>onLayout()并不是每次都被解雇。我想这可能是react native中的一个bug。谢谢@Andru,我需要用动画值设置高度<代码>onLayout()并不是每次都被解雇。我想这可能是react native中的一个bug。