<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Divam Jain</title>
    <description>The latest articles on DEV Community by Divam Jain (@divam_jain_64fbf2c056539b).</description>
    <link>https://dev.to/divam_jain_64fbf2c056539b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3036297%2F0249a976-505c-4a22-b7dc-66956a8a0068.png</url>
      <title>DEV Community: Divam Jain</title>
      <link>https://dev.to/divam_jain_64fbf2c056539b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/divam_jain_64fbf2c056539b"/>
    <language>en</language>
    <item>
      <title>How to Centralize Routes in React Like a Pro (Beginner-Friendly Guide)</title>
      <dc:creator>Divam Jain</dc:creator>
      <pubDate>Fri, 11 Apr 2025 10:48:42 +0000</pubDate>
      <link>https://dev.to/divam_jain_64fbf2c056539b/how-to-centralize-routes-in-react-like-a-pro-beginner-friendly-guide-4fm6</link>
      <guid>https://dev.to/divam_jain_64fbf2c056539b/how-to-centralize-routes-in-react-like-a-pro-beginner-friendly-guide-4fm6</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;1. Why Centralize Routes in React?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Have you ever opened a React project and found a giant, messy App.jsx cluttered with dozens of routes? Or struggled to manage different user roles (like admin vs. regular users) because routes were scattered everywhere?&lt;/p&gt;

&lt;p&gt;Centralizing your routes solves these problems by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeping your code clean&lt;/li&gt;
&lt;li&gt;Making maintenance easier&lt;/li&gt;
&lt;li&gt;Improving scalability &lt;/li&gt;
&lt;li&gt;Supporting role-based routing&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2.  Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before we dive into centralizing routes in React, make sure you have these basics covered:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Fundamentals&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Familiarity with components, JSX, and props.&lt;br&gt;
If you're new, check out the  &lt;a href="https://react.dev/learn" rel="noopener noreferrer"&gt;Learn React&lt;/a&gt; documentation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React Router Installed&lt;br&gt;
This guide uses react-router-dom v6+.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Install it in your project:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install react-router-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A React Project Set Up&lt;br&gt;
Use create-react-app, Vite, or your preferred setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basic Understanding of Routing&lt;br&gt;
Know how , , and  work.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it! If you have these ready, you’re all set to follow along.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. &lt;em&gt;Step-by-Step Implementation&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A. Folder Structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
├── router/
│   ├── UserRoutes.jsx
│   ├── AdminRoutes.jsx
│   └── AllRoutes.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;B. &lt;strong&gt;Create Route Files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;UserRoutes.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const UserRoutes = [
   {
     path:"/",
     element:&amp;lt;Home/&amp;gt;
  },
  {
    path: "/dashboard",
    element: &amp;lt;UserDashboard /&amp;gt;
  },
  {
    path: "/profile",
    element: &amp;lt;UserProfile /&amp;gt;
  }
];

export default UserRoutes;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly create AdminRoutes.jsx as UserRoutes.jsx is created&lt;/p&gt;

&lt;p&gt;C. &lt;strong&gt;Combine Routes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AllRoutes.jsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import UserRoutes from './UserRoutes';
import AdminRoutes from './AdminRoutes';
import { Routes,Route} from 'react-router-dom'
const IndexRoutes=[...UserRoutes,...AdminRoutes]

const AllRoutes=()=&amp;gt;{
return (
    &amp;lt;Routes&amp;gt;
        {IndexRoutes.map((item,index)=&amp;gt;(
          &amp;lt;Route key={index} path={item.path} element={item.element}/&amp;gt;
        ))}
    &amp;lt;/Routes&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;D.&lt;strong&gt;_ Implement in App.jsx_&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { BrowserRouter, Routes, Route } from 'react-router-dom';
import AllRoutes from './router/AllRoutes';

function App() {
  return (
    &amp;lt;BrowserRouter&amp;gt;
      &amp;lt;AllRoutes/&amp;gt;
    &amp;lt;/BrowserRouter&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
