我无法从 ListView 行下的基于类的定义中找到触发事件的正确方法。这是我到目前为止所做的
class SampleApp extends React.Component {
constructor(props){
super(props)
this.state = {
dataSource: ds.cloneWithRows(this._partners()),
}
// this._click = this._click.bind(this);
}
_click() {
console.log('clicked');
}
_renderRow(rowData) {
return (<TouchableHighlight onPress={() => {this._click();}}>
<Text style={{ fontSize:18 }}>{rowData}</Text>
</TouchableHighlight>);
}
render() {
console.log('Partners: ', this._partners() )
return (
<View style={styles.container}>
<ListView
dataSource={this.state.dataSource}
renderRow={ this._renderRow } />
</View>
);
}
}
this
里面的 onPress
不是 react 组件。我该如何解决?这是 react Playground 链接 https://rnplay.org/apps/Xd-l3w
最佳答案
试试这个。
constructor(props){
super(props)
this.state = {
dataSource: ds.cloneWithRows(this._partners()),
}
this._renderRow = this._renderRow.bind(this);
}
_renderRow()
不包含范围。重新绑定(bind)它可以解决问题。
onPress={this._click}
也可以工作,而不是将 this._click
放在另一个函数中。
这是一个 fork of your code .
关于react-native - React native - ListView行中的绑定(bind)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685618/