React Native Touchable – 4 Different Type of Touchables

4 Different Type of React Native Touchables

In this post, you will see 4 Different Type of React Native ‘Touchable’. React Native Touchable is a component to overcome the limitation of the styling of the button component. Many time it happens when we use Button we have to style it to match the theme of the application but Button does not provides Style prop which makes Touchables more important.

There are four types of Touchables. Which are

S. No. Touchable Available For
1. TouchableNativeFeedback Only For Android
2. TouchableHighlight For Android/IOS Both
3. TouchableOpacity For Android/IOS Both
4. TouchableWithoutFeedback For Android/IOS Both

1. TouchableNativeFeedback

A wrapper for making views respond properly to touches (Android only). On Android, this component uses native state drawable to display touch feedback.

To Import TouchableNativeFeedback in Code

import { TouchableNativeFeedback} from 'react-native'

Render Using

<TouchableNativeFeedback>
    <View>
        <Text>
            Label
        </Text>
    </View>
</TouchableNativeFeedback>

2. TouchableHighlight

A wrapper for making views respond properly to touches. On key down, the opacity of the wrapped view is decreased, which allows the underlying color to show through, darkening or tinting the view.

To Import TouchableHighlight in Code

import { TouchableHighlight} from 'react-native'

Render Using

<TouchableHighlight>
    <Text>
        Label
    </Text>
</TouchableHighlight>

3. TouchableOpacity

A wrapper for making views respond properly to touches. On key down, the opacity of the wrapped view is decreased, dimming it.

To Import TouchableOpacity in Code

import { TouchableOpacity} from 'react-native'

Render Using

<TouchableOpacity>
    <Text>
        Label
    </Text>
</TouchableOpacity>

4. TouchableWithoutFeedback

Don’t use unless you have a very good reason. All elements that respond to press should have visual feedback when touched but it doesn’t have any.

To Import TouchableWithoutFeedback in Code

import { TouchableWithoutFeedback} from 'react-native'

Render Using

<TouchableWithoutFeedback>
    <View>
        <Text>
            Label
        </Text>
    </View>
</TouchableWithoutFeedback>

In this example, we will see all the four Touchables. So Lets’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code

App.js

//React Native Touchable – 4 Different Type of Touchables
//https://aboutreact.com/react-native-touchable/

//import React in our code
import React from 'react';

//import all the components we are going to use
import {
  StyleSheet,
  Text,
  View,
  SafeAreaView,
  TouchableNativeFeedback,
  TouchableHighlight,
  TouchableOpacity,
  TouchableWithoutFeedback,
} from 'react-native';

const App = () => {
  const onPress = (msg) => {
    //For generating alert on buttton click
    alert('Alert for: ' + msg);
  };

  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>

        {/*Only for android remove TouchableNativeFeedback for iOS*/}
        <TouchableNativeFeedback
          onPress={
            () => onPress('TouchableNativeFeedback Pressed')
          }
          background={
            TouchableNativeFeedback.SelectableBackground()
          }>
          <View style={styles.button}>
            <Text>
              Touchable Native Feedback(Only Android)
            </Text>
          </View>
        </TouchableNativeFeedback>

        <TouchableHighlight
          style={styles.button}
          onPress={
            () => onPress('TouchableHighlight Pressed')
          }>
          <Text>
            Touchable Highlight
          </Text>
        </TouchableHighlight>

        <TouchableOpacity
          style={styles.button}
          onPress={
            () => onPress('TouchableOpacity Pressed')
          }>
          <Text>
            Touchable Opacity
          </Text>
        </TouchableOpacity>

        <TouchableWithoutFeedback
          style={styles.button}
          onPress={
            () => onPress('TouchableWithoutFeedback Pressed')
          }>
          <View style={styles.button}>
            <Text>Touchable Without Feedback</Text>
          </View>
        </TouchableWithoutFeedback>

      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    marginTop: 50,
  },
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
    width: 300,
    marginTop: 16,
  },
});

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

Download Source Code

Output Screenshot

Output in Online Emulator

That was the 4 Different Type of React Native Touchable provided by React Native Core library.

React Native Platform Touchable using react-native-platform-touchable

If you want to explore more then you can see React Native Platform Touchable which is a built-in wrapper over the various React Native Touchable.

First of all why we need that?

If we talk about TouchableNativeFeedback, it provides a Native experience but it is only supported by the Android not iOS that is why we use TouchableOpacity as an alternative solution.

In the above situation, we have two choices

  1. Either we use a common Touchable which is TouchableOpacity (Best for both platform)
  2. Identify the platform and then apply the Touchable accordingly

If you want to use the first method then it’s OK, you can proceed with that but if you want to provide the Native experience to your App users then you should go for the second one which makes the code lengthy (not much but a little bit).

react-native-platform-touchable library bought a simple component Touchable for the second case mentioned above. It is a wrapper over various Touchables which makes life very easy. You just have to install the dependency and you are good to go. It will handle all the conditions and will use the Touchable according to the platform to provide the best experience.

For iOS:

TouchableOpacity with default activeOpacity.

For Android:

TouchableNativeFeedback with background from Android app style for Android API <= 20

TouchableOpacity for Android < 5.0

In this example, we will make a simple button with a Touchable component to show the rendering of different Touchables according to a different platform. So let’s get started.

Import React Native Platform Touchable

import Touchable from 'react-native-platform-touchable';

To Make Touchable

<Touchable
  onPress={() => console.log('Button Clicked!')}
  style={{
    backgroundColor: '#FF4500',
  }}
  background={Touchable.Ripple('#FFD700')}>
  <Text
    style={{
      color: 'white',
      fontSize: 18,
      textAlign: 'center',
      padding: 15,
    }}>
    {Platform.OS === 'android'
      ? 'Device: Android \n Touchable Type: TouchableNativeFeedback'
      : 'Device: iOS \n Touchable Type: TouchableOpacity'}
  </Text>
</Touchable>

For this example, we will make a home screen with 1 button using Touchable.

Installation of Dependency for React Native Platform Touchable

To use Touchable we need to install react-native-platform-touchable dependency.

To install this open the terminal and jump into your project using

cd ProjectName

Run the following command to install

npm install react-native-platform-touchable --save

This command will copy all the dependency into your node_module directory.

Code for React Native Platform Touchable

Now Open App.js in any code editor and replace the code with the following code

App.js

//React Native Platform Touchable
//https://aboutreact.com/react-native-touchable/

//import React in our code
import React from 'react';

//import all the components we are going to use
import {Text, View, SafeAreaView, Platform} from 'react-native';

//import Touchable
import Touchable from 'react-native-platform-touchable';

const App = () => {
  const onPress = () => {
    //For generating alert on buttton click
    alert('Touchable Clicked');
  };

  return (
    <SafeAreaView style={{flex: 1}}>
      <View
        style={{
          flex: 1,
          alignItems: 'center',
          justifyContent: 'center',
          padding: 20,
        }}>
        <Touchable
          onPress={onPress}
          style={{backgroundColor: '#FF4500'}}
          background={Touchable.Ripple('#FFD700')}>
          <Text
            style={{
              color: 'white',
              fontSize: 18,
              textAlign: 'center',
              padding: 15,
            }}>
            {Platform.OS === 'android'
              ? 'Device: Android 
                 \n Touchable: TouchableNativeFeedback'
              : 'Device: iOS
                 \n Touchable: TouchableOpacity'}
          </Text>
        </Touchable>
      </View>
    </SafeAreaView>
  );
};

export default App;

Download Source Code

Output Screenshots for React Native Platform Touchable

Android

IOS

Output in Online Emulator for React Native Platform Touchable


This is how you can Make React Native Touchable using react-native-platform-touchable. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

2 thoughts on “React Native Touchable – 4 Different Type of Touchables”

    • Button has no style prop you can only change the color using the color prop. If you want to do some customization you need to use React Native Touchable. Touchables have customization options.

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.