Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

alternative way to handle React routes in a separate file

import { lazy } from 'react'

// use lazy for better code splitting, a.k.a. load faster
const Dashboard = lazy(() => import('../pages/Dashboard'))
const Forms = lazy(() => import('../pages/Forms'))
const Cards = lazy(() => import('../pages/Cards'))
const Charts = lazy(() => import('../pages/Charts'))
const Buttons = lazy(() => import('../pages/Buttons'))
const Modals = lazy(() => import('../pages/Modals'))
const Tables = lazy(() => import('../pages/Tables'))
const Page404 = lazy(() => import('../pages/404'))
const Blank = lazy(() => import('../pages/Blank'))

const routes = [
  {
    path: '/dashboard', // the url
    component: Dashboard, // view rendered
  },
  {
    path: '/forms',
    component: Forms,
  },
  {
    path: '/cards',
    component: Cards,
  },
  {
    path: '/charts',
    component: Charts,
  },
  {
    path: '/buttons',
    component: Buttons,
  },
  {
    path: '/modals',
    component: Modals,
  },
  {
    path: '/tables',
    component: Tables,
  },
  {
    path: '/404',
    component: Page404,
  },
  {
    path: '/blank',
    component: Blank,
  },
]

export default routes
Comment

define all routes in a seperate file in react

well i had the same issue a few days ago, and the solution for me was this...

This one of the routes files:

import React from 'react';
import { Route } from 'react-router-dom';
import { ComponentY } from '../components/functionalitys';

export default [
  <Route path="/appointment" component={ComponentY} exact key="create" />,
];
This another route file:

import React from 'react';
import { Route } from 'react-router-dom';
import { LoginPage, Register } from '../components/user';

export default [
  <Route path="/register" component={Register} exact key="create" />,
  <Route path="/login" component={LoginPage} exact strict key="login" />
];
And this is how I imported in the main app.js:

import routesFromFile1 from './the/route';
import routesFromFile2 from './the/other/route';

class App extends Component{
  render(){
    return(
       <div className="wrapper">
           <section className="content container-fluid">
              <Switch>  
                <Route exact path="/" component={Home} strict={true} exact={true}/>
                <Route path="/500" component={InternalServer} />
                {routesFromFile1}
                {routesFromFile2}
              </Switch>
            </section>
          </div>
        )
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: trim string and place ... javascript 
Javascript :: count length of a string javascript 
Javascript :: react progress circle 
Javascript :: angularjs 
Javascript :: javascript foreach loop array 
Javascript :: check if string Array javascript 
Javascript :: render html in js.erb 
Javascript :: create a react app 
Javascript :: angularjs show form validation errors 
Javascript :: pass text and variable in alert javascript 
Javascript :: pure component 
Javascript :: hide html elements 
Javascript :: how to update state.item[1] in state using setState? React 
Javascript :: js contenteditable button spacebar 
Javascript :: vue cli tailwind config 
Javascript :: datatable dropdown toggle not working 
Javascript :: jquery select input value empty and hasclass 
Javascript :: Add jquery in extension 
Javascript :: check if user is streaming discord js 
Javascript :: vadd vue router 
Javascript :: react extends component Increment data 
Javascript :: interpolation in js 
Javascript :: hot to set file views in nodejs 
Javascript :: angular 11 export excel with customize header 
Javascript :: check if an element is displayed jquery 
Javascript :: mongodb populate 
Javascript :: clone canvas 
Javascript :: ojs link contact page 
Javascript :: redirect with data jquery 
Javascript :: cai nodejs ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =