Skip to content

Commit 2f8d91c

Browse files
committed
study: shared state and refactor
1 parent 8b56190 commit 2f8d91c

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

src/islands/Counter.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { useSignal } from "@preact/signals";
1+
import { Signal } from "@preact/signals";
22
import { ComponentChildren } from "preact";
33
import { Button } from "../components/Button.tsx";
44
import { Card } from "../components/Card.tsx";
55

66
interface Props {
7+
count: Signal<number>;
78
children: ComponentChildren;
89
}
910

10-
export default function Counter({ children }: Props) {
11-
const count = useSignal(0);
12-
11+
export default function Counter({ count, children }: Props) {
1312
return (
1413
<Card title="Counter">
1514
<div class="flex gap-8 items-center justify-center">

src/islands/NextContentButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function NextContentButton({ name }: Props) {
99

1010
return (
1111
<a
12-
class="px-2 py-1 border-gray-500 border-2 rounded bg-white hover:bg-gray-200 transition-colors"
12+
class="align-middle select-none font-sans font-bold text-center uppercase transition-all disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none text-xs py-3 px-6 rounded-lg shadow-md shadow-gray-900/10 hover:shadow-lg hover:shadow-gray-900/20 focus:opacity-[0.85] focus:shadow-none active:opacity-[0.85] active:shadow-none bg-white text-black border-2 border-black hover:border-transparent hover:border-transparent hover:text-white hover:bg-black"
1313
href="/about"
1414
f-partial={`/partials/about/${id}`}
1515
onClick={() => (id.value += 1)}

src/routes/_middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export async function handler(
3535
}
3636

3737
const origin = req.headers.get("Origin") || "*";
38+
ctx.state.data = "myData";
3839
const resp = await ctx.next();
3940
const headers = resp.headers;
4041

@@ -49,7 +50,6 @@ export async function handler(
4950
`${HTTP_METHOD.POST}, ${HTTP_METHOD.OPTIONS}, ${HTTP_METHOD.GET}, ${HTTP_METHOD.DELETE}, ${HTTP_METHOD.PUT}`,
5051
);
5152

52-
ctx.state.data = "myData";
5353
resp.headers.set("server", "fresh server");
5454
return resp;
5555
}

src/routes/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Handlers, PageProps } from "$fresh/server.ts";
2+
import { useSignal } from "@preact/signals";
23
import { Link } from "../components/Link.tsx";
34
import Counter from "../islands/Counter.tsx";
45

@@ -12,6 +13,7 @@ export const handler: Handlers<unknown, { data: string }> = {
1213

1314
export default function Home({ data }: PageProps<string>) {
1415
const name = nameList[Math.floor(Math.random() * 5)];
16+
const count = useSignal(0);
1517

1618
return (
1719
<div class="max-w-screen-md mx-auto flex flex-col items-center justify-center">
@@ -30,9 +32,9 @@ export default function Home({ data }: PageProps<string>) {
3032
</div>
3133

3234
<div class="my-4 flex flex-col items-center justify-center">
33-
<h2 class="text-2xl font-bold">Counter Test</h2>
35+
<h2 class="text-2xl font-bold">Counter</h2>
3436
<div class="my-4">
35-
<Counter>
37+
<Counter count={count}>
3638
<p class="mt-4">This text is rendered on the server</p>
3739
</Counter>
3840
</div>

src/routes/search.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default function Page({ data }: PageProps<Data>) {
3232
<div class="mt-4">
3333
<button
3434
type="submit"
35-
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
35+
class="align-middle select-none font-sans font-bold text-center uppercase transition-all disabled:opacity-50 disabled:shadow-none disabled:pointer-events-none text-xs py-3 px-6 rounded-lg shadow-md shadow-gray-900/10 hover:shadow-lg hover:shadow-gray-900/20 focus:opacity-[0.85] focus:shadow-none active:opacity-[0.85] active:shadow-none bg-gray-900 text-white"
3636
>
3737
Search
3838
</button>

0 commit comments

Comments
 (0)