How can I add click listener in Flatlist
?
My code:
renderItem({item, index}){
return <View style = {{
flex:1,
margin: 5,
minWidth: 170,
maxWidth: 223,
height: 304,
maxHeight: 304,
backgroundColor: '#ccc',
}}/>
}
render(){
return(<FlatList
contentContainerStyle={styles.list}
data={[{key: 'a'}, {key: 'b'},{key:'c'}]}
renderItem={this.renderItem}
/>);
}
}
Update 1: I used button but it is not working in Flatlist
. However using only button instead of Flatlist
, it works. Why is it not working in Flatlist
renderItem?
_listener = () => {
alert("clicked");
}
renderItem({item, index}){
return<View>
<Button
title = "Button"
color = "#ccc"
onPress={this._listener}
/>
</View>
}
I used
TouchableWithoutFeedback
. For that, you need to add all the renderItem elements (i.e your row) into theTouchableWithoutFeedback
. Then add theonPress
event and pass the FaltList item to the onPress event.I used
TouchableOpacity
. and it's working great.This will give you click feedback. which will not be provided byTouchableWithoutFeedback
. I did the following:. . .
You need to wrap your row element (inside your renderItem method) inside
<TouchableWithoutFeedback>
tag.TouchableWithoutFeedback
takes onPress as it's prop where you can provide onPress event.For
TouchableWithoutFeedback
refer this link