2016-04-18 50 views
3

我無法找到正確的方式,以便從列表視圖的行下的基於類的定義中觸發事件。以下是我走了這麼遠React native - 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> 
    ); 
    } 
} 

thisonPress沒有提及反應成分。我該如何解決它?這是反應樂園的鏈接https://rnplay.org/apps/Xd-l3w

回答

5

試試這個。

constructor(props){ 
    super(props) 
    this.state = { 
    dataSource: ds.cloneWithRows(this._partners()), 
    } 

    this._renderRow = this._renderRow.bind(this); 
} 

_renderRow()不包含範圍。重新綁定可以解決問題。

onPress={this._click}也將工作,而不是將this._click放在另一個函數。

這裏是fork of your code

+0

不起作用。它會給你'null不是一個對象' –

+0

你使用的是哪個版本的RN? – Zidail

+0

0.21。請檢查「反應播放」鏈接。請注意,我正在使用基於類的定義 –