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

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...