{"id":16392,"date":"2023-01-19T11:09:31","date_gmt":"2023-01-19T05:39:31","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=16392"},"modified":"2026-03-05T15:48:09","modified_gmt":"2026-03-05T10:18:09","slug":"how-to-setup-react-router-v6-tutorial","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-setup-react-router-v6-tutorial\/","title":{"rendered":"How to Setup React Router v6? | Tutorial 2026"},"content":{"rendered":"\n<p><strong>Are you looking to learn how to set up React Router v6 and start routing your React applications?<\/strong> This guide will show you step-by-step <strong><em>how to setup React Router v6 and start building dynamic, single-page applications. <\/em><\/strong>With React Router v6, you can create powerful and dynamic routing solutions with ease.<\/p>\n\n\n\n<p><strong>React Router<\/strong> v6.4.0 is a library for routing in React applications and provides a routing solution that allows developers to declaratively map routes to components and manage the URL and navigation state of the application. It also provides features like <strong>route preloading, lazy loading, and query parameters.<\/strong><\/p>\n\n\n\n<p>Routing allows the user to <strong>navigate through different pages of your app without refreshing the whole page. <\/strong>With the help of routing, you can create a single-page application (SPA) that allows users to navigate through different pages of your web app without refreshing the whole page and improve the overall user experience by making your application faster, more responsive, and more dynamic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Step-by-Step Guide to Setup React Router<\/strong><\/h2>\n\n\n\n<p><em>Follow the setup instructions carefully to add an easy navigation feature to your React app with different pages.<\/em><\/p>\n\n\n\n<p><strong>Getting Started with React Router:<\/strong><\/p>\n\n\n\n<p><em>Before diving into the next section, ensure you&#8217;re solid on full-stack development essentials like front-end frameworks, back-end technologies, and database management. If you are looking for a detailed Full Stack Development career program, you can join HCL <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>GUVI\u2019s Full Stack Development Career Program<\/strong><\/a> with Placement Assistance. You will be able to master the <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-mern-stack\/\" target=\"_blank\" rel=\"noreferrer noopener\">MERN stack<\/a> (MongoDB, Express.js, React, Node.js) and build real-life projects.<\/em><\/p>\n\n\n\n<p><em>Additionally, if you want to explore JavaScript through a self-paced course, try HCL<a href=\"https:\/\/www.guvi.in\/courses\/web-development\/advanced-javascript\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/courses\/web-development\/advanced-javascript\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" target=\"_blank\" rel=\"noreferrer noopener\"> GUVI\u2019s JavaScript self-paced certification course.<\/a><\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Installing React Router<\/strong><\/h3>\n\n\n\n<p><em>To install<strong> <a href=\"https:\/\/reactrouter.com\/en\/main\" target=\"_blank\" rel=\"noopener\">React Router<\/a>,<\/strong> you&#8217;ll need Node.js installed for the npm command line tool, after which you have to run the following command:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install react-router-dom<\/code><\/pre>\n\n\n\n<p>and wait for the installation to complete.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What is npm?<\/h4>\n\n\n\n<p>npm stands for Node Package Manager, which is a library and registry for JavaScript software packages. It is free and used by more than 11 million developers worldwide.<strong>Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well<\/strong>. It also has command-line tools to help you install the different packages and manage their dependencies. <\/p>\n\n\n\n<p>If you are using <strong>Yarn<\/strong> then run the following command:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yarn add react-router-dom<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Adding React Router<\/strong><\/h3>\n\n\n\n<p>The first thing to do after installing react-router-dom is to make React Router available anywhere in your app. For this, we use a &lt;BrowserRouter&gt; that stores the current location in the browser&#8217;s address bar using clean URLs and navigates using the browser&#8217;s built-in history stack.<\/p>\n\n\n\n<p>To do this, open the index.js file in the src folder and replace the &lt;<strong><em>React.StrictMode&gt;<\/em><\/strong> with &lt;<strong><em>BrowserRouter&gt; <\/em><\/strong>by importing it from react-router-dom and then wrapping the root component in it.<\/p>\n\n\n\n<p><strong>index.js file before changes:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport React from 'react';\nimport ReactDOM from 'react-dom\/client';\nimport '.\/index.css';\nimport App from '.\/App';\nimport reportWebVitals from '.\/reportWebVitals';\n\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(\n  &lt;React.StrictMode&gt;\n    &lt;App \/&gt;\n  &lt;\/React.StrictMode&gt;\n);\n\nreportWebVitals();\n<\/code><\/pre>\n\n\n\n<p><strong>index.js file after changes:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport React from 'react';\nimport ReactDOM from 'react-dom\/client';\nimport '.\/index.css';\nimport App from '.\/App';\nimport reportWebVitals from '.\/reportWebVitals';\nimport { BrowserRouter } from 'react-router-dom';\n\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(\n  &lt;BrowserRouter&gt;\n    &lt;App \/&gt;\n  &lt;\/BrowserRouter&gt;\n);\n\nreportWebVitals();\n<\/code><\/pre>\n\n\n\n<p>So, now you can access all the features of <strong>react-router-dom<\/strong> like routers, routes, etc.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Routing and Rendering Components<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. <strong>Creating Multiple Components<\/strong><\/h4>\n\n\n\n<p>To render different components for routing, we\u2019ll create some components in our project. In your project src folder, create a new folder as Pages. In that, create some components such as <strong>Home.js<\/strong>, <strong>Courses.js<\/strong>, <strong>Live.js<\/strong>, and <strong>Contact.js<\/strong>.<\/p>\n\n\n\n<p>See the code for all the components below:<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Home.js<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\n\nfunction Home() {\n  return (\n    &lt;div&gt;Home&lt;\/div&gt;\n  )\n}\n\nexport default Home\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Contact.js<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\n\nfunction Contact() {\n  return (\n    &lt;div&gt;Contact&lt;\/div&gt;\n  )\n}\n\nexport default Contact\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Courses.js<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\n\nfunction Courses() {\n  return (\n    &lt;div&gt;Courses&lt;\/div&gt;\n  )\n}\n\nexport default Courses\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Live.js<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\n\nfunction Live() {\n  return (\n    &lt;div&gt;Live&lt;\/div&gt;\n  )\n}\n\nexport default Live\n<\/code><\/pre>\n\n\n\n<p>After creating all the component files, the project directory should look like this:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. <strong>Define routes<\/strong><\/h4>\n\n\n\n<p>Our root component is the App.js component, where our React code gets rendered initially. We will be creating all our routes in it.&nbsp;<\/p>\n\n\n\n<p>To define the routes, we are going to use two things: <strong>Routes <\/strong>and <strong>Route<\/strong>. Import both components from react-router-dom and use them to route the components. The syntax for defining the routes is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Routes&gt;\n&nbsp;&nbsp;&nbsp;&lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\n&nbsp;&nbsp;&nbsp;&lt;Route path=\"\/course\" element={&lt;Courses \/&gt;} \/&gt;\n&nbsp;&nbsp;&nbsp;&lt;Route path=\"\/live\" element={&lt;Live \/&gt;} \/&gt;\n&nbsp;&nbsp;&nbsp;&lt;Route path=\"\/contact\" element={&lt;Contact \/&gt;} \/&gt;\n&lt;\/Routes&gt;<\/code><\/pre>\n\n\n\n<p>Let\u2019s understand this syntax in detail:<\/p>\n\n\n\n<ul>\n<li><strong>&lt;Routes&gt;- <\/strong>The &lt;Routes&gt; component is used to define the different routes that are available in your application. Each individual route is defined using a &lt;Route&gt; component.<\/li>\n\n\n\n<li><strong>&lt;Route&gt;- <\/strong>The &lt;Route&gt; component takes in a path prop, which is a string that defines the URL path that the route should match.<\/li>\n\n\n\n<li><strong>path- <\/strong>The path prop defines the route path.&nbsp;<\/li>\n\n\n\n<li><strong>element- <\/strong>The component prop defines the React component to render when the route path matches the current URL.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. <strong>Link to Navigate to Components<\/strong><\/h4>\n\n\n\n<p>Now we will use the <strong>&lt;Link&gt; <\/strong>component of react-router-dom to create clickable links that change the URL of the browser and render a different component based on the new URL. <\/p>\n\n\n\n<p>This component takes in a <strong>to <\/strong>prop, which is a string that defines the URL path that the link should navigate to, and when the user clicks on a &lt;Link&gt; component, the URL of the browser will change to the path specified in the to prop. The appropriate component associated with that route will be rendered.&nbsp;<\/p>\n\n\n\n<p>In our example, we have defined a route with a path prop of &#8216;\/contact\u2019, therefore we can create a &lt;Link&gt; component with a <strong>to <\/strong>prop of &#8216;\/contact\u2019 to allow the user to navigate to the contact page. The same procedure goes for all of the components to navigate through the different pages.&nbsp;<\/p>\n\n\n\n<p>We will now use &lt;Link&gt; to navigate to different pages based on the routes and pathnames we have defined in the App component, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \".\/App.css\";\nimport React from \"react\";\nimport { Link } from \"react-router-dom\";\n\nimport Home from \"..\/src\/Pages\/Home\";\nimport Courses from \"..\/src\/Pages\/Courses\";\nimport Live from \"..\/src\/Pages\/Live\";\nimport Contact from \"..\/src\/Pages\/Contact\";\n\nfunction App() {\n  return (\n      &lt;nav&gt;\n        &lt;ul&gt;\n          &lt;Link to=\"\/\" class=\"list\"&gt;\n            Home\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/course\" class=\"list\"&gt;\n            Courses\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/live\" class=\"list\"&gt;\n            Live course\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/contact\" class=\"list\"&gt;\n            Contact\n          &lt;\/Link&gt;\n        &lt;\/ul&gt;\n      &lt;\/nav&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Final Code<\/strong><\/h2>\n\n\n\n<p><em>If you followed and coded along with us through the blog, then your <strong>App.js<\/strong> file should look like this:<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import \".\/App.css\";\nimport React from \"react\";\nimport { Link, Route, Routes } from \"react-router-dom\";\n\nimport Home from \"..\/src\/Pages\/Home\";\nimport Courses from \"..\/src\/Pages\/Courses\";\nimport Live from \"..\/src\/Pages\/Live\";\nimport Contact from \"..\/src\/Pages\/Contact\";\n\nfunction App() {\n  return (\n    &lt;div className=\"container\"&gt;\n      &lt;nav&gt;\n        &lt;ul&gt;\n          &lt;Link to=\"\/\" class=\"list\"&gt;\n            Home\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/course\" class=\"list\"&gt;\n            Courses\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/live\" class=\"list\"&gt;\n            Live course\n          &lt;\/Link&gt;\n          &lt;Link to=\"\/contact\" class=\"list\"&gt;\n            Contact\n          &lt;\/Link&gt;\n        &lt;\/ul&gt;\n      &lt;\/nav&gt;\n\n      {\/* Defining routes path and rendering components as element *\/}\n      &lt;Routes&gt;\n        &lt;Route path=\"\/\" element={&lt;Home \/&gt;} \/&gt;\n        &lt;Route path=\"\/course\" element={&lt;Courses \/&gt;} \/&gt;\n        &lt;Route path=\"\/live\" element={&lt;Live \/&gt;} \/&gt;\n        &lt;Route path=\"\/contact\" element={&lt;Contact \/&gt;} \/&gt;\n      &lt;\/Routes&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"600\" height=\"391\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2023\/01\/ezgif.com-gif-maker-2023-01-18T125505.138.gif\" alt=\"\" class=\"wp-image-16402\" title=\"\"><figcaption class=\"wp-element-caption\">React Router v6<\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>To Set up React Router in your application, is a straightforward process that can greatly enhance the user experience of your app. With React Router, you can easily define and manage routes, and create clickable links that change the URL without refreshing the whole page.<\/p>\n\n\n\n<p>At this point, you have seen how setup React Router to navigate to different pages in your web application. Not only this, but you also saw how routing works and rendered the components in react. <\/p>\n\n\n\n<p>By following these simple steps, you can add powerful routing functionality to your React application, allowing the users to easily navigate through different pages of your web app. Hope you learned a lot from this blog but don\u2019t stop learning here!<\/p>\n\n\n\n<p><em>Kickstart your Full Stack Development journey by enrolling in HCL <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>GUVI&#8217;s certified Full Stack Development Career Program<\/strong><\/a><strong> with Placement Assistance<\/strong> where you will master the MERN stack (MongoDB, Express.js, React, Node.js) and build interesting real-life projects. This program is crafted by our team of experts to help you upskill and assist you in placements. <\/em><\/p>\n\n\n\n<p><em>Alternatively, if you want to explore JavaScript through a self-paced course, try HCL <a href=\"https:\/\/www.guvi.in\/courses\/web-development\/advanced-javascript\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/courses\/web-development\/advanced-javascript\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=How+to+Setup+React+Router+v6%3F+%7C+Tutorial+2024\" target=\"_blank\" rel=\"noreferrer noopener\">GUVI\u2019s JavaScript self-paced course.<\/a><\/em><\/p>\n\n\n\n<p><strong>Sign up today and unleash your coding potential!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1692002278476\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Q1. How do I add react router v6?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>Ans<\/strong>. Here are a few steps to add react router v6:<br \/>Step 1: Installing React Router. <br \/>Step 2: Adding React Router. <br \/>Step 3: Routing and Rendering components<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1692002307639\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Q2. What is react router v6?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>Ans<\/strong>. React Router v6 is\u00a0<strong>a popular and powerful routing library for React applications<\/strong>. It provides a declarative, component-based approach to routing and handles the common tasks of dealing with URL params, redirects, and loading data.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1692002353571\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Q3. How to install react router dom in your application?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><strong>Ans<\/strong>. You just need to type the command<strong> npm install react-router-dom@6 <\/strong>in the preferred location in the console window and hit enter. This installs the react router dom of version 6 in your application and you&#8217;re good to go. <\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Are you looking to learn how to set up React Router v6 and start routing your React applications? This guide will show you step-by-step how to setup React Router v6 and start building dynamic, single-page applications. With React Router v6, you can create powerful and dynamic routing solutions with ease. React Router v6.4.0 is a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":16393,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[737],"tags":[],"views":"56395","authorinfo":{"name":"GUVI Geek","url":"https:\/\/www.guvi.in\/blog\/author\/admin\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2023\/01\/How-to-set-up-300x169.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2023\/01\/How-to-set-up.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/16392"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=16392"}],"version-history":[{"count":34,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/16392\/revisions"}],"predecessor-version":[{"id":103114,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/16392\/revisions\/103114"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/16393"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=16392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=16392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=16392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}