Display Hyperlink in React Native App for Android and iOS

Hyperlink in React Native App

In this post, we will see how to display a hyperlink in React Native App for Android and iOS. By the definition, Hyperlink is a link from a hypertext document to another location, activated by clicking on a highlighted word or image. In simple words, a simple tag with href defines a hyperlink.

In React Native if you are writing a URL/Email address in between the text then it will be treated as a text-only but in some cases, we need clickable URL/Email to open it in the browser or Email client and in this case we need a hyperlink.

Making a hyperlink in React Native is very simple and can be made using a simple library called react-native-hyperlink. This library provides a Hyperlink component and to make a text hyperlink you just have to wrap the parent Text component with the Hyperlink Component. Have a look at the below code snippets to know more about the Hyperlink component:

1. simple text which will do nothing on click of the link

<Text style={styles.sectionDescription}>
  Please click on https://aboutreact.com to check if it can do any action
</Text>

2. Text with the hyperlink, on the click of the URL it will open the URL in Browser or will open the mailbox for the email

<Hyperlink linkDefault={true}>
  <Text style={styles.sectionDescription}>
    Please click on https://aboutreact.com to check if it can do
    any action
  </Text>
</Hyperlink>

3. Text with the hyperlink which has a prop onPress to pass a function to handle the click on the URL or Email

<Hyperlink onPress={(url, text) => alert(url + ', ' + text)}>
  <Text style={styles.sectionDescription}>
    Please click on https://aboutreact.com to check if it can do
    any action
  </Text>
</Hyperlink>

4. Text with the custom style hyperlink which has a prop onPress to pass a function to handle the click on the URL or Email

<Hyperlink
  linkStyle={{ color: '#2980b9', fontSize: 16 }}
  onPress={(url, text) => alert(url + ', ' + text)}>
  <Text style={styles.sectionDescription}>
    Please click on https://aboutreact.com to check if it can do
    any action
  </Text>
</Hyperlink>

5. Text with the custom style hyperlink,  onPress prop and linkText prop to replace URL with any text

<Hyperlink
  linkStyle={{ color: '#2980b9', fontSize: 16 }}
  onPress={(url, text) => alert(url + ', ' + text)}
  linkText={url =>
    url === 'https://aboutreact.com' ? 'AboutReact' : url
  }>
  <Text style={styles.sectionDescription}>
    Please click on https://aboutreact.com to check if it can do
    any action
  </Text>
</Hyperlink>

In this example, we are going to make a single screen contains different text with and without the hyperlink. Now let’s get started with the example and see how to display a hyperlink.

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.

Installation of Dependency

To use Hyperlink component we have to install react-native-hyperlink dependency. To install the dependency open the terminal and jump into your project

cd ProjectName

Now install the dependency

npm install react-native-hyperlink --save

Code to display a hyperlink in React Native App

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

App.js

// Display Hyperlink in React Native App for Android and iOS
// https://aboutreact.com/react-native-hyperlink/

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

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

import Hyperlink from 'react-native-hyperlink';

const App = () => {
  return (
    <SafeAreaView style={{flex: 1}}>
      <ScrollView
        contentInsetAdjustmentBehavior="automatic">
        <View style={styles.container}>
          <View style={styles.sectionContainer}>
            <Text style={styles.header}>
              Example of React Native Hyperlink
            </Text>
            <Text style={styles.sectionTitle}>
              Without Hyperlink
            </Text>
            <Text style={styles.sectionDescription}>
              Please click on https://aboutreact.com to check if it
              can do any action
            </Text>

            <Text style={styles.sectionTitle}>
              Hyperlink and Click to open in Browser
            </Text>
            <Hyperlink linkDefault={true}>
              <Text style={styles.sectionDescription}>
                Please click on https://aboutreact.com to check if it
                can do any action
              </Text>
            </Hyperlink>

            <Text style={styles.sectionTitle}>
              Hyperlink and Click to get the url in onPress
            </Text>
            <Hyperlink
              onPress={(url, text) => alert(url + ', ' + text)}>
              <Text style={styles.sectionDescription}>
                Please click on https://aboutreact.com to check if it
                can do any action
              </Text>
            </Hyperlink>

            <Text style={styles.sectionTitle}>
              Hyperlink and Custom Styling of Hyperlink
            </Text>
            <Hyperlink
              linkStyle={{color: '#2980b9', fontSize: 16}}
              onPress={(url, text) => alert(url + ', ' + text)}>
              <Text style={styles.sectionDescription}>
                Please click on https://aboutreact.com to check if it
                can do any action
              </Text>
            </Hyperlink>

            <Text style={styles.sectionTitle}>
              Hyperlink, Custom styling and Replace URL with String
            </Text>
            <Hyperlink
              linkStyle={{color: '#2980b9', fontSize: 16}}
              onPress={(url, text) => alert(url + ', ' + text)}
              linkText={(url) =>
                url === 'https://aboutreact.com' ? 'AboutReact' : url
              }>
              <Text style={styles.sectionDescription}>
                Please click on https://aboutreact.com to check if it
                can do any action
              </Text>
            </Hyperlink>
          </View>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
};
export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    justifyContent: 'center',
    alignItems: 'center',
    textAlign: 'center',
    padding: 10,
  },
  header: {
    fontSize: 22,
    fontWeight: '600',
    color: 'black',
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 18,
    fontWeight: '600',
    color: 'black',
    marginTop: 30,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 15,
    fontWeight: '400',
    color: 'black',
  },
});

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

Output Screenshots

   

Output in Online Emulator


This is how you can display a hyperlink in the React Native app. 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. 🙂

Leave a Comment

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