React Routing

Routing

In a single-page application, you change what the user sees by showing or hiding portions of the display that correspond to particular components, rather than going out to the server to get a new page. As users perform application tasks, they need to move between the different views that you have defined.

To handle the navigation from one view to the next, you use the React Router. The Router enables navigation by interpreting a browser URL as an instruction to change the view. Your complete route structure is place at main.tsx file under src » main.tsx

Suppose you want to create a new module ( For creating a new module refer create new module ) then you have to add new routes for that modules.


Basic Route

Following are the fundamental building blocks to creating a route.

  1. Import the main.tsx into App.tsx and add it to the imports array.
  2. import React, { Fragment, lazy } from 'react';
    import ReactDOM from 'react-dom/client';
    import { BrowserRouter, Route, Routes } from 'react-router-dom';
            
     //component import
     import App from './pages/App.tsx';
     import Crm from './container/dashboards/crm/crm.tsx';
     import Ecommerce from './container/dashboards/ecommerce/ecommerce.tsx';
     import Crypto from './container/dashboards/crypto/crypto.tsx'
     ReactDOM.createRoot(document.getElementById('root')!).render(
            <React.Fragment>
                <BrowserRouter>
                    <React.Suspense>
                      <Routes>
                        <Route path={`${import.meta.env.BASE_URL}/`} element={<App />}>
                        <Route index element={<Crm />} />
                            <Route path={`${import.meta.env.BASE_URL}dashboards/crm`} element={<Crm />}/>
                            <Route path={`${import.meta.env.BASE_URL}dashboards/ecommerce`} element={<Ecommerce />}/>
                            <Route path={`${import.meta.env.BASE_URL}dashboards/crypto`} element={<Crypto />}/>
                        </Route>
                      </Routes>
                    </React.Suspense>
                </BrowserRouter>
            </Fragment>
     )
                            
    Configure Link in Menu

    To Add new link in Sidemenu

    Following are the fundamental building blocks to creating a new link.

    
                                    
                            ├── node_modules
                            ├── src
                            	├──components
                                     ├──common
                                       ├──sidebar
                                            ├──sidemenu
                            		           ├──sidemenu.tsx
                            		            Ex:  export const MenuItems: any = [
                                                {
                                                  menutitle: "MAIN",
                                                },
                                              
                                                {
                                                  icon: DashboardIcon,
                                                  badgetxt: badge,
                                                  title: "Dashboards",
                                                  type: "sub",
                                                  active: false,
                                                  selected: false,
                                                  children: [
                                                    {path: `${import.meta.env.BASE_URL}dashboards/crm`,type: "link",active: false,selected: false,dirchange: false, title: "CRM"},
                                                    {path:`${import.meta.env.BASE_URL}dashboards/ecommerce`,type: "link",active: false,selected: false,dirchange: false, title: "Ecommerce"},
                                                    {path: `${import.meta.env.BASE_URL}dashboards/crypto`,type: "link",active: false,selected: false,dirchange: false, title: "Crypto"},
                                                    
                                                  ],
                                                },
                                            ]