React native 当componentdidmount安装时获取组件高度
我使用0.21.0 react native, 我有一个自定义组件 对于动画,我找到了获得高度的解决方案。 宽度是使用维度获取的。但身高不一样React native 当componentdidmount安装时获取组件高度,react-native,React Native,我使用0.21.0 react native, 我有一个自定义组件 对于动画,我找到了获得高度的解决方案。 宽度是使用维度获取的。但身高不一样 如何获取组件的高度?正如@Chris Geirman在评论中指出的,您可以使用onLayout来实现此目的。下面是获取此构件高度的示例: var CustomComponent = React.createClass({ getInitialState: function() { return { componentHeight: 0 };
如何获取组件的高度?正如@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 outonLayout
。我们可能会更有用的代码来查看hank you@Andru,我需要用动画值设置高度<代码>onLayout()并不是每次都被解雇。我想这可能是react native中的一个bug。谢谢@Andru,我需要用动画值设置高度<代码>onLayout()并不是每次都被解雇。我想这可能是react native中的一个bug。