【发布时间】:2017-01-18 12:33:11
【问题描述】:
如何通过 ref 访问 react-native listview 子组件?
class MyComponent extends Component {
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(['row 1', 'row 2', 'row 3', 'row 4', 'row 5', 'row 6']),
};
}
onTap() {
console.log(this.refs)
/* After getting touchComponent refs below block of code has to be executed.
that.refs.touchComponent.measure((ox, oy, width, height, px, py) => {
this.setState({
isVisible: true,
buttonRect: {x: px, y: py, width: width, height: height}
});
});
*/
}
render() {
return (
<ListView ref="listView"
dataSource={this.state.dataSource}
renderRow={(rowData) => <TouchableHighlight ref="touchComponent" onPress={this.onTap.bind(this)}><Text>{rowData}</Text></TouchableHighlight>}
/>
);
}
}
console.log(this.refs) 打印 Object {listView: Object}
如何访问TouchableHighlight 组件引用?
我经历了React Native: Refs in ListView 和React Native - get refs of custom components in listview from renderRow,但我不明白他们是怎么做的。
【问题讨论】:
标签: react-native react-native-listview