Skip to content

Commit

Permalink
use react-router-dom for page routing instead of useState onClick
Browse files Browse the repository at this point in the history
  • Loading branch information
sehnryr committed Oct 21, 2024
1 parent b70e3e0 commit 83f1ac1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
2 changes: 2 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
"@tauri-apps/api": "npm:@tauri-apps/api@^2.0.2",
"@types/react": "npm:@types/react@^18.3.11",
"@types/react-dom": "npm:@types/react-dom@^18.3.1",
"@types/react-router-dom": "npm:@types/react-router-dom@^5.3.3",
"@vitejs/plugin-react": "npm:@vitejs/plugin-react@^4.3.2",
"react": "npm:react@^18.3.1",
"react-dom": "npm:react-dom@^18.3.1",
"react-router-dom": "npm:react-router-dom@^6.27.0",
"vite": "npm:vite@^5.4.9"
},
"nodeModulesDir": "auto"
Expand Down
41 changes: 41 additions & 0 deletions deno.lock

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

26 changes: 15 additions & 11 deletions src-www/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { useState } from "react";
import { createBrowserRouter, RouterProvider } from "react-router-dom";

import NavBar from "./components/NavBar.tsx";
import NavItem from "./components/NavItem.tsx";
import Extension from "./pages/Extensions.tsx";
import More from "./pages/More.tsx";

export default function App() {
const [page, setPage] = useState("extension");

const pages: { [key: string]: React.ReactNode } = {
extension: <Extension />,
more: <More />,
};
const router = createBrowserRouter([
{
path: "/",
element: <Extension />,
},
{
path: "/more",
element: <More />,
},
]);

export default function App() {
return (
<div
style={{
Expand All @@ -23,13 +27,13 @@ export default function App() {
}}
>
<main style={{ flex: 1 }}>
{pages[page]}
<RouterProvider router={router} />
</main>
<NavBar>
<NavItem onClick={() => setPage("extension")}>
<NavItem href="/">
<span>Extensions</span>
</NavItem>
<NavItem onClick={() => setPage("more")}>
<NavItem href="/more">
<span>More</span>
</NavItem>
</NavBar>
Expand Down
6 changes: 4 additions & 2 deletions src-www/components/NavItem.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export default function NavItem(
{ onClick, children }: { onClick: () => void; children: React.ReactNode },
{ href, children }: { href: string; children: React.ReactNode },
) {
return (
<a
onClick={onClick}
href={href}
style={{
cursor: "pointer",
display: "flex",
flexDirection: "column",
alignItems: "center",
gap: "0.25rem",
textDecoration: "none",
color: "inherit",
}}
>
{children}
Expand Down

0 comments on commit 83f1ac1

Please sign in to comment.