/
App.jsx
41 lines (38 loc) · 975 Bytes
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Link, Route, Switch } from 'react-router-dom'
// Auto generates routes from files under ./pages
// https://vitejs.dev/guide/features.html#glob-import
const pages = import.meta.glob('./pages/*.jsx', { eager: true })
const routes = Object.keys(pages).map((path) => {
const name = path.match(/\.\/pages\/(.*)\.jsx$/)[1]
return {
name,
path: name === 'Home' ? '/' : `/${name.toLowerCase()}`,
component: pages[path].default
}
})
export function App() {
return (
<>
<nav>
<ul>
{routes.map(({ name, path }) => {
return (
<li key={path}>
<Link to={path}>{name}</Link>
</li>
)
})}
</ul>
</nav>
<Switch>
{routes.map(({ path, component: RouteComp }) => {
return (
<Route key={path} path={path}>
<RouteComp />
</Route>
)
})}
</Switch>
</>
)
}