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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

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

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

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

/*Example of Text Hyperlink in React Native*/
import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';
import Hyperlink from 'react-native-hyperlink';

const App: () => React$Node = () => {
  return (
    <View>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <View style={styles.body}>
            <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>
    </View>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: 'white',
  },
  body: {
    backgroundColor: 'white',
  },
  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',
  },
});
export default App;

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

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


Related Posts

Leave a Comment

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