React 18 Router Set Up New Base URL Example Tutorial

Last Updated on by in React JS

Throughout this extensive tutorial, we will teach you how to add, create or set up a new base URL in React app using the Router DOM v6 library.

In this guide, we will throw light on following concepts: how to set up a base URL in React using React Router v6 package.

We will show you how to create simple page routes or URLs that will help you navigate from one page to another page in in React js application.

A base URL is a base or a consistent web location or address of your website.

For example, when you check the on the browser, it is the base url. And anything that comes after the becomes the URL path.

We will use React Router DOM to specify the base URL in React. Router DOM is a popular library available through the npm package registry.

It helps set up dynamic routing in a React web app.

It authorizes a user to show web pages and enables users to navigate between the web pages with the client and server-side routing in React.

Build New Application

React CRA is a generic yet best way to create a new React app globally.

Here is the command that helps you develop a new React app.

npx create-react-app react-blog-app

To manage the base url, make sure to enter into the project’s root.

cd react-blog-app

Create Function Component

In React, functionalities can be created with the class as well as functional components/. Function components are easy to create and manage; therefore, make the components folder.

Make the components/Home.js file with the following code.

import React from 'react'

function Home() {
  return (

export default Home

Then, build the components/Profile.js file with the suggested code.

import React from 'react'

function Profile() {
  return (

export default Profile

Now, make the components/Contact.js file, and put the suggested code in the file.

import React from 'react'

function Contact() {
  return (

export default Contact

Set Up React Router Dom

React Router DOM is a standard package to set up routes and url that helps you navigate within the app.

This will be the main module to help us set up the base URL, hence run the suggested command.

npm i react-router-dom --legacy-peer-deps

Define New Routes

As we said earlier, Routes allow us to navigate within the app.

In order to set up or create new Routes, we have to navigate to the components folder; here, create the Nav.js file

import { Routes, Route } from "react-router-dom";
import Home from "./Home";
import Profile from "./Profile";
import Contact from "./Contact";

const Nav = () => {
  return (
        <Route exact path="/" element={<Home />} />
        <Route path="/profile" element={<Profile />} />
        <Route path="/contact" element={<Contact />} />

export default Nav;

We added function components and Router DOM: Routes and Route modules.

The Routes modules offer a container for a nested tree of elements that renders the branch that best matches the current location.

The Route module is used to declare an element that should be rendered at a certain URL path.

Create Link Components

Navigate to App.js component then place the following code.

You have to import the Nav module and the Link module. The Link API is a public API for rendering a history-aware.

Also, it enables navigating to the associated, linked component or page.

import "../node_modules/bootstrap/dist/css/bootstrap.min.css";
import { Link } from "react-router-dom";
import Nav from "./components/Nav";

const App = () => {
  return (
    <div className="container">
      <h2>React Configure Base Path Example</h2>
      <div className="mb-5">
        <Link className="nav-link link-danger" to="/">
        <Link className="nav-link link-danger" to="profile">
        <Link className="nav-link link-danger" to="contact">
          <Nav />

export default App;

Create New Base URL

To change the base path in React, ensure that you have added the basename to the BrowserRouter module in the index.js file.

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";

import { BrowserRouter } from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"));

    <BrowserRouter basename="/positronx">
      <App />

View App on Browser

We will finally test the app on the browser and make sure the base path is changed. We are now going to run the npm start command from the console.

npm start

You can use the new base path that is given here.


React Router DOM Set Up a New Base URL Example Tutorial


Apart from adding a new base URL in React with React Router DOM. You can use the Router DOM package to set up new routes and URLs to traverse between the web pages.

Digamber - Author

Hi, I'm Digamber Singh, a New Delhi-based full-stack developer, tech author, and open-source contributor with 10+ years' experience in HTML, CSS, JavaScript, PHP, and WordPress.