How to get size of a component in react-native?

Rick

I know there's

Dimensions.get('window');

But what about a arbitrary view that has no dim string? The arbitrary view could have many subviews. The size of the view is decided by style and layout of the subviews, and it's hard to calculate the size from subviews.

Nader Dabit

You can measure a view using the onLayout function mentioned here and here. To set it up, you need to call an onLayout function, which takes an event and returns an object, which contains the nativeEvent object. This object contains the x & y coordinates, as well as the width and height of the view.

I've set up an example project implementing the code here:

https://rnplay.org/apps/mbPdZw

Below is a simple setup that measures a view:

'use strict';

var React = require('react-native');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight
} = React;

var SampleApp = React.createClass({

  getInitialState() {
        return {
            x: '',
            y: '',
            width: '',
            height: '',
            viewHeight: 100
        }
    },

  measureView(event) {
    console.log('event peroperties: ', event);
    this.setState({
            x: event.nativeEvent.layout.x,
            y: event.nativeEvent.layout.y,
            width: event.nativeEvent.layout.width,
            height: event.nativeEvent.layout.height
        })
    },

    changeHeight() {
        this.setState({
        viewHeight: 200
      })
    },

  render: function() {
    return (
      <View >
       <View onLayout={(event) => this.measureView(event)}  style={{height:this.state.viewHeight, marginTop:120, backgroundColor: 'orange'}}>
                <Text >The outer view of this text is being measured!</Text>
            <Text>x: {this.state.x}</Text>
            <Text>y: {this.state.y}</Text>
            <Text>width: {this.state.width}</Text>
            <Text>height: {this.state.height}</Text>
        </View>

        <TouchableHighlight style={{flexDirection:'row', alignItems: 'center', justifyContent: 'center', padding:15, backgroundColor: '#ddd', marginTop:10}} onPress={() => this.changeHeight() }>
              <Text style={{fontSize:18}}>Change height of container</Text>
        </TouchableHighlight>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 28,
    textAlign: 'center',
    margin: 10,
  }
});

AppRegistry.registerComponent('SampleApp', () => SampleApp);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get a react component's size (height/width) before render?

From Dev

React Native Swift UI Component frame size

From Dev

How to get onPress event from ScrollView Component in React Native

From Dev

How to get data from the redux store and use it in component in React Native

From Dev

React Native - Get the position of a component in the View

From Dev

react native get component height when componentdidmount

From Dev

react native get component height when componentdidmount

From Dev

React Native : set id to a component and get this component's id

From Dev

react - how do I get the size of a component child elements and reposition them

From Java

How to Pass Props to Modal Component in React Native

From Dev

How to achieve 'FrameLayout' component in React Native?

From Dev

React Native: How does this Component call?

From Dev

How to achieve 'FrameLayout' component in React Native?

From Dev

How to render component with ajax in react native?

From Dev

react native: How to listen to component position?

From Dev

How to pass array of View/Component in react native?

From Dev

How to change a string in React-Native into a Component?

From Dev

React Native - How to measure size of content in ScrollView?

From Dev

How can I get the state of child checkbox components within a parent ListView component using React-Native?

From Dev

How to get reference to the dispatch method from a component method in my react-native redux app

From Dev

How get native image size at runtime?

From Dev

how to change Class component to functional component in react native

From Dev

React Native get Clicked(pressed) component's attribute

From Dev

React native custom component

From Dev

Importing a native React Component

From Dev

in a react component, how to get `this` in static function?

From Dev

How to get Ref to component in scalsjs-react?

From Dev

How do I get this to refer to the React component?

From Dev

React: How to get results from one component to another component

Related Related

  1. 1

    How to get a react component's size (height/width) before render?

  2. 2

    React Native Swift UI Component frame size

  3. 3

    How to get onPress event from ScrollView Component in React Native

  4. 4

    How to get data from the redux store and use it in component in React Native

  5. 5

    React Native - Get the position of a component in the View

  6. 6

    react native get component height when componentdidmount

  7. 7

    react native get component height when componentdidmount

  8. 8

    React Native : set id to a component and get this component's id

  9. 9

    react - how do I get the size of a component child elements and reposition them

  10. 10

    How to Pass Props to Modal Component in React Native

  11. 11

    How to achieve 'FrameLayout' component in React Native?

  12. 12

    React Native: How does this Component call?

  13. 13

    How to achieve 'FrameLayout' component in React Native?

  14. 14

    How to render component with ajax in react native?

  15. 15

    react native: How to listen to component position?

  16. 16

    How to pass array of View/Component in react native?

  17. 17

    How to change a string in React-Native into a Component?

  18. 18

    React Native - How to measure size of content in ScrollView?

  19. 19

    How can I get the state of child checkbox components within a parent ListView component using React-Native?

  20. 20

    How to get reference to the dispatch method from a component method in my react-native redux app

  21. 21

    How get native image size at runtime?

  22. 22

    how to change Class component to functional component in react native

  23. 23

    React Native get Clicked(pressed) component's attribute

  24. 24

    React native custom component

  25. 25

    Importing a native React Component

  26. 26

    in a react component, how to get `this` in static function?

  27. 27

    How to get Ref to component in scalsjs-react?

  28. 28

    How do I get this to refer to the React component?

  29. 29

    React: How to get results from one component to another component

HotTag

Archive