Skip to content Skip to sidebar Skip to footer

React-native: Cannot Access State Values Or Any Methods Declared From Renderrow

I have a listview, whenever user clicks on an item, i want to do something with onPress (touchable highlight). I tried defining function then calling them in onPress but they didnt

Solution 1:

How to solve your problem

Change

  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderRow}
    style={styles.listView}
  />

To

  <ListView
    dataSource={this.state.dataSource}
    renderRow={data => this.renderRow(data)}
    style={styles.listView}
  />

Or

  <ListView
    dataSource={this.state.dataSource}
    renderRow={this.renderRow.bind(this)}
    style={styles.listView}
  />

Why?

As you already know(since you are trying to solve the problem by using arrow function and bind), to call renderRow, the caller should be in the same context as the component itself. But this connection is lost in renderRow={this.renderRow}, which created a new function(new context).

Post a Comment for "React-native: Cannot Access State Values Or Any Methods Declared From Renderrow"