React Native ListView的Item设置点击事件时null is not an object

先贴下代码:

<ListView 
                    contentContainerStyle = {styles.list}
                    pageSize={2}
                    dataSource = {this.state.dataSource}
                    renderRow={this._renderRow}
                />

_renderRow(rowData) {
        console.log('rowData.url: '+ rowData.url) ;
        console.log('rowData.name: '+ rowData.name) ;
        return (
            <TouchableOpacity onPress = {() =>this._pressRow(rowData.url)} underlayColor = "transparent" >
            <View>
            <View style={styles.row}>
            <Text style={styles.text}> {rowData.name} Text>
             View>
            View>
            TouchableOpacity>
        )
    }

ListView里的renderRow={this._renderRow},然后会报错,显示null is not an object (evaluating '_this3._pressRow'),上图:
React Native ListView的Item设置点击事件时null is not an object_第1张图片
一看就知道是找不到方法,后来查阅这个资料,知道了解决方法,原来renderRow要与外面的this绑定起来,这个跟Android里的上下文环境Context很像:

{styles.list}
                    pageSize={2}
                    dataSource = {this.state.dataSource}
                    renderRow={this._renderRow.bind(this)}
                />

http://stackoverflow.com/questions/36303634/reactnative-tapping-row-in-listview-gives-error-cannot-read-property-pressr

你可能感兴趣的:(React,Native)