0

Im new to react-native. Im trying to read x,y co-ordinates of a view inside its parent. Using below code

componentDidMount: function() {
  setTimeout(this.measureView);
},
measureView: function() {
  this.refs.completeView.measure((ox, oy, width, height) => {
    this.setState({headerHeight: height});
  });
},
render : function(){
return (
  <TouchableHighlight onPress={() => this.props._pressRow(this.props.currentRowID)}>
    <View style={styles.container}
          ref="completeView">
      <View style={styles.colorView}>
      </View>
      <View style={styles.textWrapper}>
        <Text style={styles.text}>{this.props.text}</Text>
      </View>
    </View>
  </TouchableHighlight>
);
},

Inside measureView, this.refs is always undefined. Why?

I'm trying to read last view's co-ordinates into ListView, because for some reason I want to render the last row. So is there any other solution for this?

Please also make a note that this class is being exported into its parent class.

3
  • Can you show us the code for View please? Commented May 11, 2016 at 7:06
  • If you are talking about render function, then I just updated the code. @MatthewHerbst Commented May 11, 2016 at 7:07
  • @Rajesh did you find the solution? I have the same issue. Commented Oct 6, 2016 at 14:17

2 Answers 2

1

Try using ES2015 lambda's to get the correct lexical value of this within methods.

measureView = () => {
  this.refs.completeView.measure((ox, oy, width, height) => {
    this.setState({headerHeight: height});
  });
},
Sign up to request clarification or add additional context in comments.

Comments

1

set ref using a function instead of a string:

var myComponent = null;

...

componentDidMount: function() {
  setTimeout(this.measureView);
},
measureView: function() {
  this.myComponent.completeView.measure((ox, oy, width, height) => {
    this.setState({headerHeight: height});
  });
},
render : function(){
return (
  <TouchableHighlight onPress={() => this.props._pressRow(this.props.currentRowID)}>
    <View style={styles.container}
          ref={((component)=>this.myComponent = component)}>
      <View style={styles.colorView}>
      </View>
      <View style={styles.textWrapper}>
        <Text style={styles.text}>{this.props.text}</Text>
      </View>
    </View>
  </TouchableHighlight>
);
},

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.