Skip to content

Cannot get 'this' in <ListView> renderRow function #967

Description

@ReadingSteiner

I wrote a function called renderMyRow to render rows in a ListView, but I found it is strange that I can't get this in this function.

Then I fixed it by add a bind(this) in my code, but i wonder why i cannot get 'this' in my renderMyRow ?

here is the code

classSearchResultextendsComponent{constructor(props){super(props);vardataSource=newListView.DataSource({rowHasChanged: (r1,r2)=>r1.guid!==r2.guid});this.state={dataSource: dataSource.cloneWithRows(this.props.listings)};}render(){return(<ListViewdataSource={this.state.dataSource}renderRow={this.renderMyRow}/>)}rowPressed(propertyGuid){varproperty=this.props.listings.filter(prop=>prop.guid===propertyGuid)[0];this.props.navigator.push({title: 'Property',component: PropertyView,props: {property: property}})}renderMyRow(rowData,sectionID,rowID){console.log(this);// Output: nullvarprice=rowData.price_formatted.split(' ')[0];return(<TouchableHighlightunderlayColor='#DDDDDD'onPress={()=>this.rowPressed(rowData.guid)}><View><Viewstyle={styles.rowContainer}><Imagestyle={styles.thumb}source={{uri : rowData.img_url}}/><Viewstyle={styles.textContainer}><Textstyle={styles.price}>£{price}</Text><TextnumberOfLines={1}>{rowData.title}</Text></View></View><Viewstyle={styles.separator}/></View></TouchableHighlight>);}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions