ChangJoo Park
ChangJoo Park

Reputation: 979

react native get component height when componentdidmount

I use 0.21.0 react native, I have one custom component.

for animated, I find solution to get height. width is get using Dimension. but height is not same.

How do I get height of my component?

Upvotes: 1

Views: 6813

Answers (1)

Andru
Andru

Reputation: 6204

As @Chris Geirman pointed out in the comment, you can use onLayout for this. Here an example to get the height of this component:

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});
  }
});

Now you have access to the height of the component as this.state.componentHeight.

Upvotes: 4

Related Questions