Skip to content

Commit 8ea3dbe

Browse files
authored
refactor(www): use useSignal() instead of useState() (#2894)
1 parent d2da471 commit 8ea3dbe

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

docs/latest/examples/client-side-components-and-libraries.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
5353
<p>Leaflet must be loaded on the client. No children will render</p>
5454
);
5555
}
56-
const [value, setValue] = useState<typeof Leaflet | null>(null);
56+
const value = useSignal<typeof Leaflet | null>(null)
5757
return (
5858
<>
5959
{/* Load Leaflet CSS */}
@@ -65,7 +65,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
6565
/>
6666
{/* Load Leaflet JS */}
6767
<script
68-
onLoad={() => setValue(window.L)}
68+
onLoad={() => value.value = window.L}
6969
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
7070
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
7171
crossorigin=""
@@ -120,8 +120,9 @@ export default function MapIsland() {
120120
```tsx MapIsland.tsx
121121
import * as Leaflet from "https://esm.sh/v135/@types/leaflet@1.9.4/index.d.ts";
122122
import { IS_BROWSER } from "$fresh/runtime.ts";
123-
import { useContext, useEffect, useState } from "preact/hooks";
123+
import { useContext, useEffect } from "preact/hooks";
124124
import { ComponentChildren, createContext } from "preact";
125+
import { useSignal } from "@preact/signals";
125126

126127
// Create a context to hold Leaflet data/functions
127128
const LeafletContext = createContext<typeof Leaflet | null>(null);
@@ -131,7 +132,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
131132
if (!IS_BROWSER) {
132133
return <p>Leaflet must be loaded on the client. No children will render</p>;
133134
}
134-
const [value, setValue] = useState<typeof Leaflet | null>(null);
135+
const value = useSignal<typeof Leaflet | null>(null);
135136
return (
136137
<>
137138
{/* Load Leaflet CSS */}
@@ -143,7 +144,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
143144
/>
144145
{/* Load Leaflet JS */}
145146
<script
146-
onLoad={() => setValue(window.L)}
147+
onLoad={() => value.value = window.L}
147148
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
148149
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
149150
crossorigin=""

www/islands/TableOfContents.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { useEffect, useRef, useState } from "preact/hooks";
1+
import { useEffect, useRef } from "preact/hooks";
22
import type { MarkdownHeading } from "../utils/markdown.ts";
3+
import { useSignal } from "@preact/signals";
34

45
export interface TableOfContentsProps {
56
headings: MarkdownHeading[];
@@ -33,7 +34,7 @@ function setActiveLink(
3334
export function TableOfContents({ headings }: TableOfContentsProps) {
3435
const ref = useRef<HTMLDivElement>(null);
3536
const refMarker = useRef<HTMLDivElement>(null);
36-
const [isOpen, setIsOpen] = useState(false);
37+
const isOpen = useSignal(false);
3738

3839
useEffect(() => {
3940
if (!ref.current) return;
@@ -105,7 +106,7 @@ export function TableOfContents({ headings }: TableOfContentsProps) {
105106
<button
106107
type="button"
107108
id="toc-outline-btn"
108-
onClick={() => setIsOpen((v) => !v)}
109+
onClick={() => isOpen.value = !isOpen.value}
109110
class="bg-background-primary py-2 px-4 rounded border border-foreground-secondary/30 flex items-center hover:border-fresh-green/80 transition-colors text-sm"
110111
>
111112
On this page

www/islands/ThemeToggle.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import { useState } from "preact/hooks";
21
import { IS_BROWSER } from "fresh/runtime";
2+
import { useSignal } from "@preact/signals";
33

44
export default function ThemeToggle() {
5-
const [theme, setTheme] = useState(() => {
6-
if (!IS_BROWSER) return "light";
7-
return document.documentElement.dataset.theme ?? "light";
8-
});
5+
const theme = useSignal(
6+
!IS_BROWSER ? "light" : document.documentElement.dataset.theme ?? "light",
7+
);
98

109
const toggleTheme = () => {
11-
setTheme((prev) => {
12-
const theme = prev === "light" ? "dark" : "light";
13-
document.documentElement.setAttribute("data-theme", theme);
14-
localStorage.setItem("theme", theme);
15-
return theme;
16-
});
10+
theme.value = theme.value === "light" ? "dark" : "light";
11+
document.documentElement.setAttribute("data-theme", theme.value);
12+
localStorage.setItem("theme", theme.value);
1713
};
1814

1915
return (
@@ -23,7 +19,7 @@ export default function ThemeToggle() {
2319
class="dark-mode-toggle button p-1 -m-1"
2420
aria-label="Toggle Theme"
2521
>
26-
{theme === "light"
22+
{theme.value === "light"
2723
? (
2824
<svg
2925
class="fill-foreground-primary hover:fill-fresh w-6 h-6"

0 commit comments

Comments
 (0)