Make a Long URL Short and Share from React Native App

This post contains How to Make a Long URL Short and Share from React Native App. We will see what is a URL shortener, how it works and how it can help you? So let’s get started.

If you guys have observed the text messages closly then you have seen that nobody sends the original URL of their product or service in the message nowadays instead they send a short URL from bit.ly or any other URL shortener. URL shorteners are very famous nowadays. It helps you to shorten your URL, you just have to provide the long URL to the API and in return, it provides you a new short URL to share. This way you can save your text character and your audience can see the message clearly instead of long redundant URL.

There are many URL Shortener sites on the internet to do this job for free and TinyURL is one of them which we are going to use in this example. This URL shorteners can help you a lot in many ways like:

  1. It provides a sweet and beautiful small URL.
  2. It reduces the character used for the URL in the text message.
  3. Some sites also provide the analytics about the click on the link and the user behavior as whenever user clicks the URL they reach your site using these short URLs.
  4. You can properly convey your message to the audience instead of a big lengthy URL.

Other than that you can also find your ways how can you make it useful to you.

To Shorten the URL

To Shorten a URL you just have to find a URL Shortner API and have to pass the URL with the parameter they want. I have used TinyURL for this example and here is how I have to pass my long URL

In response to that, it provides me the short URL as text

Look how simple it was. now let’s move towards the code and see how to call an HTTP call to get the short URL and use a Share API provided by React Native to share that URL with Message.

In this example, we are going to make a screen with 1 button and on the click of the button we will call the URL shortener to provide a short URL, after getting the short URL we will add a short message with the response URL and will call a Share API to share the message on all the possible channels available.

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.

Code to Make a Long URL Short and Share

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 to make a long URL short and share from 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.