我正在尝试附加一个方法来接收传递到 ListView 的数据。具体来说,我正在利用 ListView React Native 组件及其 _renderRow 函数将数据源映射到不同的行。在数据源中,我想将其中一个数据元素作为参数传递给此方法:
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>
)
}
}
那么,正确的做法是什么?我想将 DataSource 数据传递给 onPress 方法应该是相当常见的,但我在网上找不到任何东西。
谢谢!
最佳答案
在渲染行中的 onPress 中只需执行以下操作:
onPress={this.someMethod}
这应该可以解决问题。
关于javascript - react native : Passing DataSource into Method from Props in _renderRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38447362/