this refers to the wrong context, you need a lexical binding region, and this is what the thick arrow function does for you.
Try calling your function as follows:
onPress={ () => this._handlePress() }
In addition, you need to bind the map function to the correct context, for example:
<View style={styles.albums}>
{
list.map(function(item, index){
return (
<TouchableOpacity key={index} onPress={() => this._handlePress()}>
<Text>{item}</Text>
</TouchableOpacity>
)
}.bind(this))
}
</View>
Or like this:
<View style={styles.albums}>
{
list.map((item, index) => {
return (
<TouchableOpacity key={index} onPress={() => this._handlePress()}>
<Text>{item}</Text>
</TouchableOpacity>
)
})
}
</View>
I created a working draft here .
https://rnplay.org/apps/_PmG6Q
source
share