I am trying to attach a method to take in the data passed into a ListView. Specifically, I am utilizing the ListView React Native component and it's _renderRow function that maps a dataSource to different rows. Within the datasource, I would like to pass in one of the data elements as an argument to this method:
class MainComponent extends Component {
constructor(props) {
super(props)
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = { dataSource: ds.cloneWithRows([['foo1', 'bar1'], ['foo2','bar2']])};
this._renderRow = this._renderRow.bind(this);
}
render() {
return (
<View>
<ListView
dataSource={this.state.dataSource} // [['foo1', 'bar1'], ['foo2', 'bar2']]
renderRow={this._renderRow}
/>
</View>
);
}
_renderRow(rowData) {
return (
<SomeComponent onPress={_ => this.someMethod} data={rowData} />
//Trying to call someMethod with argument 'bar1'.
//I have also tried passing in the argument here with:
// onPress={_ => this.someMethod({rowData[1]})}
)
}
someMethod(data) {
//do something with data
}
}
class SomeComponent extends Component {
render() {
return (
<TouchableHighlight
underlayColor='#EFEFEF'
onPress={this.props.onPress(this.props.data[1])}>
//Is this the correct way to pass in 'bar1' into the method?
<View>
<Text>{this.props.data[0]}</Text>
</View>
</TouchableHighlight>
)
}
}
So, what is the correct way to do this? I imagine that it should be fairly common to want to pass in the DataSource data to the onPress method, but I wasn't able to find anything online.
Thanks!