Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
How to Build a URL Shortener

How to Build a URL Shortener in React with Shrtcode

Posted on September 6, 2023January 1, 2026 By Updategadh No Comments on How to Build a URL Shortener in React with Shrtcode

Create Your Own Viral URL Shortener in React with Shrtcode – How to Build a URL Shortener in React with Shrtcode

“How Does URL Shortening Work ?”

The method of URL shortening entails encoding the original link and assigning it a shorter, random character sequence that is recorded in a database. When a user clicks on the shortened URL, the server uses this random sequence to get the original link and redirects the user to it. This approach, of course, demands the creation of a backend system.

We will use the capabilities of the Shrtcode API in this guide to streamline this functionality, avoiding the need for us to delve into the subtleties of backend operations.”

How to Build a URL Shortener in React with Shrtcode
How to Build a URL Shortener
  • Create Your Own Viral URL Shortener in React with Shrtcode – How to Build a URL Shortener in React with Shrtcode
  • “How Does URL Shortening Work ?”
  • Pros, How to Build a URL Shortener:
  • Cons ,How to Build a URL Shortener:
  • How to Build a URL Shortener in React with Shrtcode
    • Set up React (
      • Step-1 : Spin up a and set React sandbox on CoderPad:
      • Step-2 :Replace the information in App.tsx in the newly formed sandbox.
      • Step-3 Next, modify App.css:
    • How to shorten URLs with Shrtcode
    • Copy shortened URL to the clipboard
  • Complete App.tsx Code

As acclaimed author Michael Lewis, known for his work on “Moneyball,” wisely remarked, “There’s something unfavorable in every advantage and something advantageous in every drawback.” Let’s delve into the merits and demerits of URL shorteners.

Pros, How to Build a URL Shortener:

  1. Facilitate Sharing: URL shorteners simplify the sharing process. They reduce the length of URLs, making it more convenient for users to copy, paste, or share them, particularly in situations where character limits apply, such as messaging apps or printed materials.
  2. User-Friendly URLs: Most URL shorteners offer the option to create custom links, replacing long and cryptic URLs with concise, intuitive, and memorable addresses. This customization enhances user experience and aids in brand recognition.
  3. Analytics Insights: Many URL shortening services provide analytics features, enabling users to track link performance. This data includes valuable information on click-through rates and traffic sources, empowering businesses to refine their content distribution strategies.

Cons ,How to Build a URL Shortener:

  1. Risk of Dead Links: URL shorteners are susceptible to the stability of the server used to create the shortened links. If the server experiences downtime or ceases to function, all associated links become inactive, potentially causing frustration for users.
  2. Blocked URLs: Some online platforms and security measures restrict the acceptance of shortened URLs due to their association with spam and phishing attempts. Consequently, certain URL shorteners have found themselves on spam blacklists, limiting their usability.
  3. Phishing Vulnerability: The inherent obscurity of shortened URLs, often represented by random characters, can be exploited by cybercriminals for phishing attacks. Scammers use these deceptive links to redirect users to seemingly legitimate websites designed to collect sensitive information, posing a significant security risk.

How to Build a URL Shortener in React with Shrtcode

The URL shortener we will create will include an input field for the original, long URL as well as a part for seeing the shortened URL. It will also contain the ability to copy the shortened URL to the clipboard, which will be accomplished through the use of react-copy-to-clipboard. Let’s get this party started:

Set up React (

Step-1 : Spin up a and set React sandbox on CoderPad:

How to Build a URL Shortener in React with Shrtcode
How to Build a URL Shortener

Step-2 :Replace the information in App.tsx in the newly formed sandbox.

import { useState } from 'react';
import './App.css';

function App() {
  const [url, setUrl] = useState()
  const [shortendUrl, setShortenedUrl] = useState('')

  return (
    <div className="app">
      <div className='shortener'>
        <h2>URL shortener</h2>
        {/* form to enter URL to be shortened */}
        <form>
          <input
            placeholder='Enter URL'
            value={url}
            onChange={(e) => setUrl(e.target.value)}/>
          <button>Submit</button>
        </form>
        {/* Section to view shortened URLS */}
        {shortendUrl &&
          <div className='shortener__viewShot'>
            {shortendUrl}
          </div>
        }
      </div>
    </div>
  );
}

export default App;

We created a url to manage the input field and a shortendUrl state to store the abbreviated URL in the code block above.

Step-3 Next, modify App.css:

* {
  box-sizing: border-box;
}
.shortener {
  width: fit-content;
  margin: 0 auto;
  text-align: center;
}
.shortener input, button {
  height: 30px;
  padding: 0 10px;
}
.shortener input {
  border: 1px solid gray;
  color: rgb(64, 64, 66);
}
.shortener input:focus {
  border: 1px solid rgb(95, 180, 245);
  outline: none;
}
.shortener form > button {
  background-color: rgb(95, 180, 245);
  border: none;
  color: white;
}
.shortener form > button:hover {
  background-color: rgb(51, 162, 246);
}
.shortener__viewShot {
  margin-top: 20px;
  border: 1px solid gainsboro;
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  color: rgb(64, 64, 66);
  align-items: center;
}
.shortener__viewShot button {
  background-color: transparent;
  border: none;
  color: gray;
}
.shortener__viewShot button:hover {
  color: rgb(64, 64, 66);
}

The preview panel displays the modifications made by the sandbox and the URL shortener application:

How to shorten URLs with Shrtcode

“In order to create shortened URLs using Shrtcode, simply provide the URL as a query parameter to the designated API endpoint as detailed in their documentation. Proceed to the ‘App.tsx’ file and incorporate the following function following the ‘shortenedUrl’ state within the ‘App’ component:”

const shortenUrl = async (e) => {
    e.preventDefault();
    try {
      const response = await fetch(
        `https://api.shrtco.de/v2/shorten?url=${url}`
      )
      const data = await response.json()
      setShortenedUrl(data.result.full_short_link);
    } catch (e) {
      alert(e);
    }
};

In the function mentioned earlier, we appended the original URL as a query parameter to Shrtcode’s shortening endpoint and initiated a GET request to that endpoint. Upon receiving the response, it is stored in the ‘shortenedUrl’ state variable.

Moving forward, to invoke this function when the Submit button is clicked, please make the following modification to the opening form tag within the App component:

<form onSubmit={shortenUrl}>
How to Build a URL Shortener in React with Shrtcode
How to Build a URL Shortener

Copy shortened URL to the clipboard

Next, we’ll implement the functionality to copy the shortened URL to the clipboard when the copy button is clicked. To achieve this, we’ll incorporate the ‘react-copy-to-clipboard’ library. In the sandbox’s shell console, install this library by running the following command:

npm i react-copy-to-clipboard

Next, our next step involves passing the button responsible for triggering the copy function as a child component to the CopyToClipboard component. Additionally, we need to transmit the short URL to be copied as properties (props).

To accomplish this, we must begin by importing the CopyToClipboard component from the ‘react-copy-to-clipboard’ library. Following that, we should make the following modifications to the div element with the class name ‘shortener__viewShot’:

import {CopyToClipboard} from 'react-copy-to-clipboard';



<div className="shortener__viewShot">
  {shortendUrl}
  <CopyToClipboard text={shortendUrl}>
    <button onClick={() => alert("The URL has been copied")}>copy</button>
  </CopyToClipboard>
</div>

Once the copy button is activated within our application, the shortened URL will be swiftly copied to the clipboard, prompting the app to generate and display an alert confirming this action.

Complete App.tsx Code

import { useState } from 'react';
import {CopyToClipboard} from 'react-copy-to-clipboard';
import './App.css';

function App() {
  const [url, setUrl] = useState()
  const [shortendUrl, setShortenedUrl] = useState('')

  const shortenUrl = async (e) => {
    e.preventDefault();
    try {
      const response = await fetch(
        `https://api.shrtco.de/v2/shorten?url=${url}`
      )
      const data = await response.json()
      setShortenedUrl(data.result.full_short_link);
    } catch (e) {
      alert(e);
    }
  };

  return (
    <div className="app">
      <div className='shortener'>
        <h2>URL shortener</h2>
        {/* form to enter URL to be shortened */}
        <form onSubmit={shortenUrl}>
          <input
            placeholder='Enter URL'
            value={url}
            onChange={(e) => setUrl(e.target.value)}/>
          <button>Submit</button>
        </form>
        {/* Section to view shortened URLS */}
        {shortendUrl &&
          <div className="shortener__viewShot">
          {shortendUrl}
          <CopyToClipboard text={shortendUrl}>
            <button onClick={() => alert("The URL has been copied")}>copy</button>
          </CopyToClipboard>
        </div>
        }
      </div>
    </div>
  );
}

export default App;
Post Views: 1,452
Javascript Project Tags:Java ScriptJspPython, JavaScript

Post navigation

Previous Post: Resume for Freshers: How to Make a Resume for Freshers
Next Post: How to pass Google interview?

More Related Articles

E-Waste Facility Locator E-Waste Facility Locator Javascript Project
Video Chat App Video Chat App (Node.js + Express + Socket.io) Javascript Project
How To Create an Icon Bar with Html CSS - Image 10 How To Create an Icon Bar with Html CSS Javascript Project

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. How to Create a CSS Page Loading Spinner
  2. Secrets How to Design Digital Clock using JavaScript !
  3. Build a Signature Pad in HTML, CSS, JS & Canvas
  4. How to Create a Promo Code using html css Js (Free Source Code)
  5. File Sharing App With Free Source Code
  6. E-Waste Facility Locator

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Blog Site In PHP And MYSQL With Source Code || Best Project
  9. Online Bike Rental Management System Using PHP and MySQL
  10. E learning Website in php with Free source code
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Real-Time Medical Queue & Appointment System with Django
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code

Most Viewed Posts

  • Top Large Language Models in 2025 (8,616)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,226)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,875)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme