Setup a Single URL for App Store and Play Store in a React App

Maintaining a website for a mobile application is a standard. We can direct to the store URL from our website. But if we are building a mobile app for a global audience, Android and iOS versions are necessary. We can direct to different stores by detecting the mobile OS. Here we will see how to set up a single URL for an App Store and Play Store in a React app.

Why a Single URL?

Assume that, one person is asking for the link to our mobile app. We don’t know whether he is an Android user or an iOS user. It prompts us to send two separate URLs, one for the Apps Store and another for the iOS.

The above problem is applicable if the user reaches our website. In most cases, we will show two buttons. One for the App Store and another one for the Play Store.

If the UI of our website consists of only a single button called Download now, then we must set up a route or page that detects the OS the person is using and redirect to the corresponding store.

The below representation shows the workflow of a food delivery app’s website.

Setup Single URL for App Store and Play Store in a Normal Website

Normally a website is a collection of HTML files. Each link returns an HTML file.

An HTML file consists of a head section and a body section.

To set up a single URL for App Store and Play Store on a normal website, add the below script inside the head section.

    $(document).ready(function (){
        if(navigator.userAgent.toLowerCase().indexOf("android") > -1){
            window.location.href = '';
        else if(navigator.userAgent.toLowerCase().indexOf("iphone") > -1){
            window.location.href = '';
        }else {
            window.location.href = '';

We can see an example on the website. There we can see a Download Now Button at the bottom. Clicking the button will direct us to the app.html where we will add the script in the head section.

If we are using an Android device, the app.html redirects to the Play Store link and if it is an iOS device, it redirects to the App Store link.

Setup Single URL for App Store and Play Store in a React App

A React application consists of a single HTML file named index.html and all the components are rendered on top of it. So, adding a script inside the header to detect the OS and redirect corresponding to it is not a good method.

Because adding the script in the header section of the index.html will redirect to the corresponding store from the home route itself.

So let’s learn the method of setting up a single URL for the App Store and Play Store in a React App.

1. Create a React App

After installing Node.js and NPM, create a React application using the create-react-app tool. Here, I am using single-url as the project name.

npx create-react-app single-url

This will create a new react project on our system. Open the project on VS code and start the project with the below command.

npm start

2. Setup routes

Create two routes using the react-router-dom package. Install the package first and change the index.js file in the src directory.

npm i react-router-dom

Import the package in the index.js file and create two routes listed below.

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Switch } from "react-router-dom"
import Home from './Home';
import Download from './Download';
import reportWebVitals from './reportWebVitals';

        <Route path="/" exact component={() => <Home />} />
        <Route path="/download" exact component={() => <Download />} />


3. Setup Home Component

Now set up a Home component to load with the route path or path “/”.

import './App.css';
import {Link} from "react-router-dom"

function Home() {
  return (
    <div className="home">
      <div className= "button-container">
        <Link className = "button" to="/download">
          Download Now

export default Home;

This will create a button called Download Now on the home page.

4. Download Component

Setup another component called Download to load with /download route. In our app, we will call the /download route by clicking Download Now button on our home page.

We can find our device OS with the help of a package react-device-detect.

The isAndroid and isIOS API from react-device-detect returns the OS is android or IOS respectively.

We will use these values to direct to the Play Store or App Store.

If the value isAndroid is true, it will redirect to the Play Store and in the same manner, it redirects to App Store if the variable isIOS is true.

If the device is not Android or iOS, it redirects to the route URL.

import React, {useEffect} from 'react';
import {
} from "react-device-detect";

function App() {

  useEffect(() => {
    if (isAndroid) {
      window.location.href =
    }else if(isIOS) {
      window.location.href =
    } else{
      window.location.href =
  }, []);

  return (
    <div className="App">

export default App;

Refer the GitHub Repository

I have uploaded this project to a GitHub repository for further reference.


So here in this article, we discussed the method of setting up a single URL for the App Store and Play Store in a React App.

Be the first to reply

Leave a Reply

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

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