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

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...

16 AWS Gotchas

16 AWS Gotchas In January I launched the MVP for my own startup,  Proximistyle , which helps you find what you’re looking for nearby. On advice from friends and industry contacts I chose AWS as my cloud provider. Having never had to set up my own cloud infrastructure before, the learning curve to get from no experience to a stable VPC system I was happy with was significantly steeper than expected, and had its fair share of surprises. #1 Take advantage of the free resources offered AWS offers a free tier for new accounts. If you have recently bought a domain and set up a company you qualify for the free tier for a year. Additionally, if you are a bootstrapped startup you can apply for  the Startup Builders package  and get $1000 in AWS credits. After doing the above, you’re now ready to get started with setting up the AWS infrastructure for your startup. #2 Set up billing budgets and alerting The very first thing you should do after setting up billing, is enabling a budge...

Ultimate Folder Structure For Your React Native Project

  Ultimate Folder Structure For Your React Native Project React native project structure React Native is a flexible framework, giving developers the freedom to choose their code structure. However, this can be a double-edged sword for beginners. Though it offers ease of coding, it can soon become challenging to manage as your project expands. Thus, a structured folder system can be beneficial in many ways like better organization, simplified module management, adhering to coding practices, and giving a professional touch to your project. This write-up discusses a version of a folder arrangement that I employ in my React Native projects. This structure is based on best practices and can be modified to suit the specific needs of your project. Before we get into the project structure let’s give credit to @sanjay who has the original idea of the structure but I modify his version of the code, to make it better. Base library axios  — For network calling. react-navigation ...