refactor(www): use useSignal() instead of useState() (#2894)

This commit is contained in:
Asher Gomez
2025-05-12 23:28:46 +02:00
committed by GitHub
parent d2da471d67
commit 8ea3dbeac5
3 changed files with 18 additions and 20 deletions
@@ -53,7 +53,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
<p>Leaflet must be loaded on the client. No children will render</p>
);
}
const [value, setValue] = useState<typeof Leaflet | null>(null);
const value = useSignal<typeof Leaflet | null>(null)
return (
<>
{/* Load Leaflet CSS */}
@@ -65,7 +65,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
/>
{/* Load Leaflet JS */}
<script
onLoad={() => setValue(window.L)}
onLoad={() => value.value = window.L}
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""
@@ -120,8 +120,9 @@ export default function MapIsland() {
```tsx MapIsland.tsx
import * as Leaflet from "https://esm.sh/v135/@types/leaflet@1.9.4/index.d.ts";
import { IS_BROWSER } from "$fresh/runtime.ts";
import { useContext, useEffect, useState } from "preact/hooks";
import { useContext, useEffect } from "preact/hooks";
import { ComponentChildren, createContext } from "preact";
import { useSignal } from "@preact/signals";
// Create a context to hold Leaflet data/functions
const LeafletContext = createContext<typeof Leaflet | null>(null);
@@ -131,7 +132,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
if (!IS_BROWSER) {
return <p>Leaflet must be loaded on the client. No children will render</p>;
}
const [value, setValue] = useState<typeof Leaflet | null>(null);
const value = useSignal<typeof Leaflet | null>(null);
return (
<>
{/* Load Leaflet CSS */}
@@ -143,7 +144,7 @@ function LeafletProvider(props: { children: ComponentChildren }) {
/>
{/* Load Leaflet JS */}
<script
onLoad={() => setValue(window.L)}
onLoad={() => value.value = window.L}
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""