Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native : measure a View

I'm trying to measure a view using a ref, but the width returned is 0 despite the fact that I see the view displayed on the screen (and it's far from being equal to 0).

I tried to follow this : https://github.com/facebook/react-native/issues/953 but it just doesn't work ... I just want to get the actual width of the View.

Here is my code :

var Time = React.createClass({
  getInitialState: function() {
    return({ progressMaxSize: 0 }); 
  },
  componentDidMount: function() {
    this.measureProgressBar();
  },
  measureProgressBar: function() {
    this.refs.progressBar.measure(this.setWidthProgressMaxSize);
  },
  setWidthProgressMaxSize: function(ox, oy, width, height, px, py) {
    this.setState({ progressMaxSize: width });
  },
  render: function() {
    return(
      <View style={listViewStyle.time} ref="progressBar">
        <View style={listViewStyle.progressBar} >
          <View style={[listViewStyle.progressMax, { width: this.state.progressMaxSize }]}/>
        </View>
      </View>
    );
  }
}); 

The styles associated :

time: {
    position: 'absolute',
    bottom: 5,
    left: 5,
    right: 5,
    backgroundColor: '#325436',
    flexDirection: 'row',
  },
  progressBar: {
    flex: 1,
    backgroundColor: '#0000AA',
  },
  progressMax: {
    position: 'absolute',
    top: 0,
    left: 0,
    backgroundColor: '#000',
    height: 30, 
  },
like image 851
ilansas Avatar asked Apr 23 '15 16:04

ilansas


People also ask

How do you get the size of a view in React Native?

To get the size of a View in React Native, we can get them from the parameter of the onLayout callback. For instance, we write: import * as React from 'react'; import { View, StyleSheet } from 'react-native'; import { Text } from 'react-native-paper'; const MyComponent = () => { const [dims, setDims] = React.

How do you get the position of an element in React Native?

To get the position of an element with React Native, we can get it from the View 's onLayout callback. to add a View with the onLayout prop set to a function that gets the position values from the event. nativeEvent. layout property.

How do you get parent view height in React Native?

To get the parent height and width in React: Set the ref prop on the element. In the useEffect hook, update the state variables for the height and width. Use the offsetHeight and offsetWidth properties to get the height and width of the element.

What is setNativeProps?

setNativeProps is the React Native equivalent to setting properties directly on a DOM node. caution. Use setNativeProps when frequent re-rendering creates a performance bottleneck! Direct manipulation will not be a tool that you reach for frequently.


1 Answers

To measure view size, no need to use ref at all. You can get it from nativeEvent passed by onLayout.

measureView(event: Object) {
   console.log(`*** event: ${JSON.stringify(event.nativeEvent)}`);
   // you'll get something like this here:
   // {"target":1105,"layout":{"y":0,"width":256,"x":32,"height":54.5}}
}

render() {
    return (
        <View onLayout={(event) => {this.measureView(event)}} />
    );
}
like image 64
nopopon Avatar answered Oct 06 '22 08:10

nopopon