Chethan N
Chethan N

Reputation: 1158

How to listen to a touch event in React Native?

I need to listen to touch event of a React Native view and do some action in the callback. I couldn't get any way of doing it in a simple way. Only workaround I could find is to use Pan Responder and use onResponderGrant callback. Wanted to know if there is a simpler way of listening to touch event, something similar to onPress.

Upvotes: 3

Views: 3035

Answers (1)

Ryan Turnbull
Ryan Turnbull

Reputation: 3944

You can use TouchableOpacity as a View that provides touchable feedback. Eg

<TouchableOpacity
    onPress = {() => function()}
    onLongPress = {() => longerFunction()}
    style = {{these can be the same as a normal View}}>
</TouchableOpacity>

You can also use TouchableHighlight and TouchableWithoutFeedback, which provide a darkening onPress effect and no effect respectively.

Upvotes: 5

Related Questions