Skip to content

Commit

Permalink
feat: update routes
Browse files Browse the repository at this point in the history
  • Loading branch information
QinZhen001 committed Dec 23, 2023
1 parent 37caa66 commit 6b142fb
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 44 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"@videojs/http-streaming": "^2.12.1",
"m3u8-parser": "^4.7.0",
"react": "^18.0.2",
"react-dom": "^18.0.2",
"react-router-dom": "^6.2.1",
"scheduler": "0.20.2",
"video.js": "^7.17.0",
Expand Down
9 changes: 5 additions & 4 deletions src/pages/main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Link, Outlet } from "react-router-dom";
import { routes, PageRoute } from "../../router";
import { routes } from "../../router";
import { PageRoute } from "../../types";
import "./index.css";

export const MainPage = () => {
Expand All @@ -10,7 +11,7 @@ export const MainPage = () => {
<ul>
{finalRoutes.map(({ path, title }) => (
<li key={path}>
<Link to={path}>{title}</Link>
{path ? <Link to={path}>{title}</Link> : title}
</li>
))}
</ul>
Expand All @@ -23,9 +24,9 @@ export const CommonPageRouter = ({ routes }: { routes: PageRoute[] }) => {
<div className="page">
<section className="left">
<ul>
{routes.filter(item => !item.hidden).map(({ title, path, element }) => (
{routes.filter(item => !item.hidden && !item.index).map(({ title, path, element }) => (
<li key={path}>
<Link to={path}>{title}</Link>
{path ? <Link to={path}>{title}</Link> : title}
</li>
))}
</ul>
Expand Down
19 changes: 16 additions & 3 deletions src/pages/react/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { CommonPageRouter } from "../main"
import { Suspense, lazy } from "react"
import { PageRoute } from "../../types"


const SimpleRedux = lazy(() => import('./redux'));
const Scheduler = lazy(() => import('./scheduler'));
const UseTransition = lazy(() => import('./useTransition'));
const UseDeferredValue = lazy(() => import('./useDeferredValue'));
const TestSuspense = lazy(() => import('./suspense'));
const TestSwr = lazy(() => import('./swr'));
const AHooks = lazy(() => import('./ahooks'));
const Dob = lazy(() => import('./dob'));
const UseCountdown = lazy(() => import('./useCountdown'));
const UseCountdown = lazy(() => import('./useCountdown'));


export const children = [
export let children: PageRoute[] = [
{
path: "redux",
element: <SimpleRedux></SimpleRedux>,
title: "redux 简单实现",
},
{
path: "scheduler",
element: <Scheduler></Scheduler>,
Expand Down Expand Up @@ -55,6 +61,13 @@ export const children = [
}
]

// TIP: first element is default page,show as default router in outlet
children.unshift({
index: true,
element: children[0].element,
title: "default",
})

const ReactPage = () => {
return <CommonPageRouter routes={children}></CommonPageRouter>
};
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/pages/redux/index.tsx → src/pages/react/redux/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import arrayThunk from "./mini-redux/custom-redux-array";

let store = createStore(counterReducer, applyMiddleware(thunk, arrayThunk));

export const ReduxPage = () => {
export const SimpleRedux = () => {
return (
<div>
<Provider store={store}>
Expand All @@ -19,4 +19,4 @@ export const ReduxPage = () => {
};


export default ReduxPage
export default SimpleRedux
File renamed without changes.
File renamed without changes.
File renamed without changes
64 changes: 31 additions & 33 deletions src/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ import { children as AnimateChildren } from "../pages/animation"
import { children as CanvasChildren } from "../pages/canvas"
import { children as WebpackChildren } from "../pages/webpack"
import { Suspense, lazy } from "react"
import { HashRouter, Route, Routes } from "react-router-dom";
import { HashRouter, Route, Routes, } from "react-router-dom";
import { PageRoute } from "../types";

export type PageRoute = {
path: string;
element: React.ReactNode;
title: string;
hidden?: boolean;
children?: PageRoute[];
};

const ReactPage = lazy(() => import('../pages/react'));
const VuePage = lazy(() => import('../pages/vue'));
const WebpackPage = lazy(() => import('../pages/webpack'));
const ReduxPage = lazy(() => import('../pages/redux'));
const NodePage = lazy(() => import('../pages/node'));
const VitePage = lazy(() => import('../pages/vite'));
const MainPage = lazy(() => import('../pages/main'));
Expand All @@ -47,11 +40,6 @@ export const routes: PageRoute[] = [
title: "react 相关",
children: ReactChildren
},
{
path: "/redux",
element: <ReduxPage></ReduxPage>,
title: "redux 相关",
},
{
path: "/vite",
element: <VitePage></VitePage>,
Expand Down Expand Up @@ -95,25 +83,35 @@ export const routes: PageRoute[] = [
},
]

export const RouteContainer = () => (
<HashRouter>
<Routes>
{routes
.map((item) => (
<Route key={item.path} path={item.path} element={
<Suspense fallback={<div>Loading...</div>}>{item.element}</Suspense>
export const RouteContainer = () => {

const genRoutes = (routes: PageRoute[]) => {
let finalRoutes = routes
.map((item) => {
const { path, element, children, index } = item
return !index ? <Route key={path} path={path} element={
<Suspense
fallback={<div > Loading...</div >}>
{element}
</Suspense >
}
>
{
children && children.length
? genRoutes(children)
: null
}
>
{item.children
? item.children
.map(({ path, element }) => (
<Route key={path} path={path} element={
<Suspense fallback={<div>Loading...</div>}>{element}</Suspense>
}></Route>
))
: null}
</Route>
))}
</Route > :
<Route key="index" index element={element} />
})

return finalRoutes
}


return <HashRouter>
<Routes>
{genRoutes(routes)}
</Routes>
</HashRouter>
);
}
8 changes: 8 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type PageRoute = {
path?: string;
element: React.ReactNode
title: string;
hidden?: boolean;
index?: boolean;
children?: PageRoute[];
};

0 comments on commit 6b142fb

Please sign in to comment.