mirror of https://github.com/usememos/memos
feat: use `react-router`
parent
4608894e56
commit
307483e499
@ -0,0 +1,47 @@
|
|||||||
|
import { createBrowserRouter } from "react-router-dom";
|
||||||
|
import { userService } from "../services";
|
||||||
|
import Auth from "../pages/Auth";
|
||||||
|
import Explore from "../pages/Explore";
|
||||||
|
import Home from "../pages/Home";
|
||||||
|
|
||||||
|
const router = createBrowserRouter([
|
||||||
|
{
|
||||||
|
path: "/",
|
||||||
|
element: <Home />,
|
||||||
|
loader: async () => {
|
||||||
|
try {
|
||||||
|
await userService.initialState();
|
||||||
|
} catch (error) {
|
||||||
|
// do nth
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/auth",
|
||||||
|
element: <Auth />,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/u/:userId",
|
||||||
|
element: <Home />,
|
||||||
|
loader: async () => {
|
||||||
|
try {
|
||||||
|
await userService.initialState();
|
||||||
|
} catch (error) {
|
||||||
|
// do nth
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: "/explore",
|
||||||
|
element: <Explore />,
|
||||||
|
loader: async () => {
|
||||||
|
try {
|
||||||
|
await userService.initialState();
|
||||||
|
} catch (error) {
|
||||||
|
// do nth
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
export default router;
|
@ -1,11 +0,0 @@
|
|||||||
import Home from "../pages/Home";
|
|
||||||
import Auth from "../pages/Auth";
|
|
||||||
import Explore from "../pages/Explore";
|
|
||||||
|
|
||||||
const appRouter = {
|
|
||||||
"/auth": <Auth />,
|
|
||||||
"/explore": <Explore />,
|
|
||||||
"*": <Home />,
|
|
||||||
};
|
|
||||||
|
|
||||||
export default appRouter;
|
|
@ -1,20 +0,0 @@
|
|||||||
import appRouter from "./appRouter";
|
|
||||||
|
|
||||||
// just like React-Router
|
|
||||||
interface Router {
|
|
||||||
[key: string]: JSX.Element | null;
|
|
||||||
"*": JSX.Element | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const routerSwitch = (router: Router) => {
|
|
||||||
return (pathname: string) => {
|
|
||||||
for (const key of Object.keys(router)) {
|
|
||||||
if (key === pathname) {
|
|
||||||
return router[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return router["*"];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const appRouterSwitch = routerSwitch(appRouter);
|
|
Loading…
Reference in New Issue