Skip to content

Commit

Permalink
update example 98 for waku 0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Sep 12, 2023
1 parent dcaa071 commit eedccf5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 168 deletions.
2 changes: 1 addition & 1 deletion examples/98_router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react": "18.3.0-canary-7118f5dd7-20230705",
"react-dom": "18.3.0-canary-7118f5dd7-20230705",
"react-server-dom-webpack": "18.3.0-canary-7118f5dd7-20230705",
"waku": "0.14.0"
"waku": "0.15.0"
},
"devDependencies": {
"@types/react": "^18.2.21",
Expand Down
4 changes: 2 additions & 2 deletions examples/98_router/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ServerRouter } from "../lib/server.js";

import { routeTree } from "../routes.js";

const App = ({ path = "/" }) => {
return <ServerRouter rootTree={routeTree} path={path} />;
const App = ({ path }: { path: string }) => {
return <ServerRouter rootTree={routeTree} path={path.slice(1)} />;
};

export default App;
13 changes: 0 additions & 13 deletions examples/98_router/src/entries.ts

This file was deleted.

14 changes: 14 additions & 0 deletions examples/98_router/src/entries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { lazy } from "react";

import { defineEntries } from "waku/server";

const App = lazy(() => import("./components/App.js"));

export default defineEntries(
// renderEntries
async (input) => {
return {
App: <App path={input} />,
};
},
);
26 changes: 19 additions & 7 deletions examples/98_router/src/lib/client.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
"use client";

import { createContext, useContext, useState } from "react";
import { createContext, useCallback, useContext } from "react";
import type { ReactNode } from "react";

import { serve } from "waku/client";
import { Root, Slot, useRefetch } from "waku/client";

import { RouteTree } from "./common.js";

const RouterContext = createContext<{
setPath: (path: string) => void;
} | null>(null);

const App = serve<{ path: string }>("App");
function InnerClientRouter() {
const refetch = useRefetch();
const setPath = useCallback(
(path: string) => {
refetch("_" + path);
},
[refetch],
);
return (
<RouterContext.Provider value={{ setPath }}>
<Slot id="App" />
</RouterContext.Provider>
);
}

export function ClientRouter(props: { rootTree: RouteTree }) {
const initialPath = props.rootTree.root.path;
const [path, setPath] = useState(initialPath);
return (
<RouterContext.Provider value={{ setPath }}>
<App path={path} />
</RouterContext.Provider>
<Root initialInput={"_" + initialPath}>
<InnerClientRouter />
</Root>
);
}

Expand Down
147 changes: 2 additions & 145 deletions pnpm-lock.yaml

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

0 comments on commit eedccf5

Please sign in to comment.