Issue
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>
}
Solution
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
Answered By - Raj Suvariya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.