Display Hyperlink in React Native App for Android and iOS

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

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

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

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

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

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

Now install the dependency

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

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

   


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.