Skip to content

measureLayoutRelativeToParent broken in ListView in 0.8.0-rc #2023

@mrspeaker

Description

@mrspeaker

I was using UIManager.measureLayoutRelativeToParent to get the scroll offset of an element contained in a ListView (click on the element, and the view would scroll so the element was at the top of the screen). This stopped working between 0.7.1 and 0.8.0-rc (I can't find exactly which commit).

Here's some code showing it. Click on an element and it should log the Y offset - it works in 0.7.1, but always returns the first element's offset in 0.8.0-rc. Also, it works as expected in 0.8.0-rc if it's a ScrollView, not a ListView.

var React = require('react-native');
var UIManager = require('NativeModules').UIManager;
var { AppRegistry, StyleSheet, ListView, Text, TouchableOpacity,} = React;

var Row = React.createClass({
  measure: function () {
    var handle = React.findNodeHandle(this.refs['scrolly']);
    UIManager.measureLayoutRelativeToParent(handle, () => {}, (x, y) => {
      // Should be the y offset of the element. 
      // Works correctly in 0.7.1, 
      // always returns the first element offset in 0.8.0-rc.
      console.log(y); 
    });
  },
  render: function() {
    return (<TouchableOpacity ref={`scrolly`} onPress={this.measure}>
      <Text style={styles.row}>{this.props.txt}</Text>
    </TouchableOpacity>);
  }
});

var dataSource = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
dataSource = dataSource.cloneWithRows([...new Array(50)].map((_, i) => {{txt: 'txt' + i}}));
var test = React.createClass({
  render: function() {
    return (<ListView
      dataSource={dataSource}
      renderRow={row => <Row txt={'test'} />}>
    </ListView>);
  }
});

var styles = StyleSheet.create({
  row: { fontSize: 30, textAlign: 'center', margin: 10, },
});

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions