Maybe you can use this code.
The below code is inspired by nextjs
page routing.
You just add your routes on ~/pages/
and it imports them dynamically.
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
function DynamicRoutes() {
return (
<BrowserRouter>
<Switch>
<Route
path="/"
render={({ history, location, match })=>{
const Page = React.lazy(()=>{
return import('./pages'+location.pathname).catch((e) => {
if (/not find module/.test(e.message)) {
return import("./pages/NotFound");
}
if (/Loading chunk d+ failed/.test(e.message)) {
window.location.reload();
return;
}
throw e;
})
});
return (
<React.Suspense fallback ={<div>Loading...</div>}>
<Page />
</React.Suspense>
)
}}
/>
</Switch>
</BrowserRouter>
)
}
export default DynamicRoutes;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…