How to install and use react-router-dom in react js?

Hi Friends 👋,

Welcome To aGuideHub!

To install and use react-router-dom in React js, you have to use the npm or yarn package manager that is how you can install the react-router-dom library into your project.

Today, I am going to show you, how to install and use react-router-dom in react js.

install and use moment

Install the following packages to use react-router-dom in react js.

npm

npm install -react-router-dom

yarn

yarn add react-router-dom 

Table of contents

  • Set up the React project.
  • Import necessary components.
  • Create the file.

Step 1: Set up the React project.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import necessary components.

After installing react-router-dom, you have to import your React component. To do this, add the following line to the top of your component file.

import { Routes, Route, Link } from "react-router-dom"

Step 3: Create the file.

Now, create a new folder in src called Components. Within this folder, create 3 files.

  • Home.js
  • About.js
  • Contact.js

Home.js

import React from 'react'

export default function Home() {
	return (
		<div>
			<h1>Home</h1>
		</div>
	)
}

About.js

import React from 'react'

export default function about() {
	return (
		<div>
			<h1>About</h1>
		</div>
	)
}

Contact.js

import React from 'react'

export default function contact() {
	return (
		<div>
			<h1>Contact</h1>
		</div>
	)
}

And import 'react-dom/client' in the index.js file, then put <browser router><app /></browser router> inside root.render.

index.js

import { StrictMode } from "react";
import { createRoot } from "react-dom/client";

import App from "./App";
import { BrowserRouter } from "react-router-dom";
const rootElement = document.getElementById("root");
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>

  </StrictMode>
);

React react-router-dom example.

The code below is an example of this, Now, import the needed components for the demo inside App.js. Then add the following code in App.js. Here we are first importing the 3 pages in App.js.

App.js

import { Routes, Route, Link } from "react-router-dom"
// Import the pages
import Home from "./components/home"
import About from "./components/about"
import Contact from "./components/contact"

// Import css
import "./App.css"

function App() {
return (
	<div className="App">
		<Routes>
		<Route exact path="/" element={<h>Home Page</h>} />
		<Route exact path="home" element={<Home />} />
		<Route exact path="about" element={<About />} />
		<Route exact path="contact" element={<Contact />} />
		</Routes>
		<div className="list">
		<ul>
			<li><Link to="/">Home</Link></li>
			<li><Link to="home">Home </Link></li>
			<li><Link to="about">About</Link></li>
			<li><Link to="contact">Contact</Link></li>
		</ul>
		</div>
	</div>
);
}
export default App;

App.css

You can improve the design and make the app more presentable using the following CSS. Add it to App.css.

* {
	padding: 0;
	margin: 0;
}

h1 {
	text-align: center;
	font-size: 45px;
	font-family: Arial, Helvetica, sans-serif;
	color: rgb(6, 0, 32);
	padding: 40px;
}

.list {
	display: flex;
	justify-content: center;
	width: 100%;
}

.list ul li {
	list-style: none;
	margin: 42px;
	text-align: center;
}

a {
	text-decoration: none;
	color: rgb(0, 0, 0);
	font-size: 18px;
	font-family: Arial, Helvetica, sans-serif;
	padding: 14px 25px;
	background-color: transparent;
	border: 2px solid rgb(12, 0, 66);
}

a:hover {
	background-color: rgb(12, 0, 66);
	color: rgb(255, 255, 255);
}

Check the output of the above code example.

React, react-router-dom

Here, we are provided code sandbox links for the above program install and use react-router-dom in React js. Then you can use whenever you went and do the changes as per your requirements.

Try it Yourself

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts