Skip to main content

React Native Touchables

Touchable components provide the capability to capture the tapping functionality. The touchables component can be implemented as an alternative of basic button, if they are not look right for your app. Using these components, you build your own button. Tapping on these components, you can display the feedback.
The touchables components do not provide any default styling, so you will need to do your style for presenting nicely in the app.

Types of Touchable Components

There are four touchable components provided by React Native. Selection of this component depends on the kind of feedback you want to provide:

  • TouchableHighlight
  • TouchableNativeFeedback
  • TouchableOpacity
  • TouchableWithoutFeedback.

React Native TouchableHighlight

The TouchableHighlight can be used where you would use a button or link on the web. The background of this component becomes dark on pressing it.

Props

PropsTypeRequiredPlatformDescription
activeOpacitynumbernoDetermines the opacity of wrapped view when it is touched.
onHideUnderlayfunctionnoCalls instantly after the underlay is hidden.
onShowUnderlayfunctionnoCalls instantly after the underlay is shown.
styleView.styleno
underlayColorcolornoShow the underlay color when the touch is active.
hasTVPreferredFocusboolnoiOSIt focuses TV preferred, work only for iOS.
tvParallaxPropertiesobjectnoiOSIt is an object with properties which control the Apple TV parallax effects.

React Native TouchableNativeFeedback

The TouchableNativeFeedback makes a view to response properly on touch. This component works only for Android operating system. It uses native state drawable to display the touch feedback.
It supports only a single View instance as a child node. It is implemented by replacing the View with another instance of RCTView node.

Props

PropsTypeRequiredDescription
backgroundbackgroundPropTypenoIt determines the background drawable that is going to be displayed as feedback.
useForegroundboolnoIt adds the ripple effect to the foreground of the view, instead of the background.

React Native TouchableOpacity

The TouchableOpacity wrapper is used to reduce the opacity of button. It allows background to be seen while the user press down. The opacity of button will be controlled by wrapping the children in an Animation.

Props

PropsTypeRequiredPlatformDescription
activeOpacitynumbernoIt determines the opacity of wrapped view when it is touched.
tvParallaxPropertiesobjectnoiOSIt is an object with property which is used to control the Apple TV parallax effects.
hasTVPreferredFocusboolnoiOSIt focuses TV preferred, it works on Apple TV only.

Methods

MethodDescription
setOpacityTo()It animates the touchable to a new opacity.

React Native TouchableWithoutFeedback

The TouchableWithoutFeedback is used when the user wants to handle the tap functionality but doesn't want to display any feedback.

Props

PropsTypeRequiredDescription
hitSlopobjectnoThis defines how far your touch can start away from the button.
onAccessibilityTapfunctionnoIf accessible is set to true, the system invokes this function when the user performs accessibility tap gesture.
accessibilityHintstringnoIt helps user to understand what will happen when they perform an action on the accessibility element.
accessibilityLabelnodenoIt overrides the text, which is read by the screen reader when users interact with the element.
delayLongPressnumbernoIt delays the onLongPress in milli-second calling onPressIn.

Some time user presses a view and holds it for the set of time. This long press is handled by the function using onLongPress props of any of the above "Touchable" components.

React Native Touchables Example

  1. import React, { Component } from 'react';  
  2. import { Alert,Platform,StyleSheet,Text,TouchableHighlight,TouchableOpacity,  
  3.     TouchableNativeFeedback,TouchableWithoutFeedback, View } from 'react-native';  
  4.   
  5. export default class Touchables extends Component {  
  6.     _onPressButton() {  
  7.         Alert.alert('You tapped the button!')  
  8.     }  
  9.   
  10.     _onLongPressButton() {  
  11.         Alert.alert('You long-pressed the button!')  
  12.     }  
  13.   
  14.   
  15.     render() {  
  16.         return (  
  17.             <View style={styles.container}>  
  18.                 <TouchableHighlight onPress={this._onPressButton} underlayColor="white">  
  19.                     <View style={styles.button}>  
  20.                         <Text style={styles.buttonText}>TouchableHighlight</Text>  
  21.                     </View>  
  22.                 </TouchableHighlight>  
  23.                 <TouchableOpacity onPress={this._onPressButton}>  
  24.                     <View style={styles.button}>  
  25.                         <Text style={styles.buttonText}>TouchableOpacity</Text>  
  26.                     </View>  
  27.                 </TouchableOpacity>  
  28.                 <TouchableNativeFeedback  
  29.                     onPress={this._onPressButton}  
  30.                     background={Platform.OS === 'android' ? TouchableNativeFeedback.SelectableBackground() : ''}>  
  31.                     <View style={styles.button}>  
  32.                         <Text style={styles.buttonText}>TouchableNativeFeedback</Text>  
  33.                     </View>  
  34.                 </TouchableNativeFeedback>  
  35.                 <TouchableWithoutFeedback  
  36.                     onPress={this._onPressButton}  
  37.                 >  
  38.                     <View style={styles.button}>  
  39.                         <Text style={styles.buttonText}>TouchableWithoutFeedback</Text>  
  40.                     </View>  
  41.                 </TouchableWithoutFeedback>  
  42.                 <TouchableHighlight onPress={this._onPressButton} onLongPress={this._onLongPressButton} underlayColor="white">  
  43.                     <View style={styles.button}>  
  44.                         <Text style={styles.buttonText}>Touchable with Long Press</Text>  
  45.                     </View>  
  46.                 </TouchableHighlight>  
  47.             </View>  
  48.         );  
  49.     }  
  50. }  
  51.   
  52. const styles = StyleSheet.create({  
  53.     container: {  
  54.         paddingTop: 60,  
  55.         alignItems: 'center'  
  56.     },  
  57.     button: {  
  58.         marginBottom: 30,  
  59.         width: 260,  
  60.         alignItems: 'center',  
  61.         backgroundColor: '#5ead97'  
  62.     },  
  63.     buttonText: {  
  64.         padding: 20,  
  65.         color: 'white',  
  66.         fontSize: 18  
  67.     }  
  68. });  
Output:
React Native Touchables React Native Touchables
React Native Touchables

Comments

Popular Posts

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...