From 8fa4141e06cfd53b7521c88a7f14d12ed99aed5c Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 2 Jun 2023 06:43:01 +1000 Subject: [PATCH] refactor: replace `useState()` with `useSignal()` (#1234) --- docs/concepts/islands.md | 7 ++- docs/getting-started/adding-interactivity.md | 15 +++--- init.ts | 17 ++++--- tests/fixture/import_map.json | 4 +- tests/fixture/islands/Counter.tsx | 9 ++-- tests/fixture/islands/RootFragment.tsx | 8 ++-- .../RootFragmentWithConditionalFirst.tsx | 8 ++-- tests/fixture/islands/folder/Counter.tsx | 9 ++-- .../islands/kebab-case-counter-test.tsx | 9 ++-- tests/fixture/routes/islands/index.tsx | 12 +++-- .../islands/CheckDuplication.tsx | 27 +++++------ .../islands/InsertCssrules.tsx | 35 +++++++------- www/components/gallery/CodeBox.tsx | 8 ++-- www/islands/CopyArea.tsx | 17 +++---- www/islands/LemonDrop.tsx | 47 ++++++++++--------- 15 files changed, 120 insertions(+), 112 deletions(-) diff --git a/docs/concepts/islands.md b/docs/concepts/islands.md index e196e5fd..7c571bea 100644 --- a/docs/concepts/islands.md +++ b/docs/concepts/islands.md @@ -14,15 +14,14 @@ island. The file must have a default export that is a regular Preact component. ```tsx // islands/MyIsland.tsx -import { useState } from "preact/hooks"; +import type { Signal } from "@preact/signals"; export default function MyIsland() { - const [count, setCount] = useState(0); + const count = useSignal(0); return (
- Counter is at {count}.{" "} - + Counter is at {count}.
); } diff --git a/docs/getting-started/adding-interactivity.md b/docs/getting-started/adding-interactivity.md index 981e1c7e..767ec13e 100644 --- a/docs/getting-started/adding-interactivity.md +++ b/docs/getting-started/adding-interactivity.md @@ -31,7 +31,8 @@ Here is an example of an island component that counts down to a specific time. ```tsx // islands/Countdown.tsx -import { useEffect, useState } from "preact/hooks"; +import { useSignal } from "@preact/signals"; +import { useEffect } from "preact/hooks"; const timeFmt = new Intl.RelativeTimeFormat("en-US"); @@ -39,18 +40,16 @@ const timeFmt = new Intl.RelativeTimeFormat("en-US"); // props to island components need to be JSON (de)serializable. export default function Countdown(props: { target: string }) { const target = new Date(props.target); - const [now, setNow] = useState(new Date()); + const now = useSignal(new Date()); // Set up an interval to update the `now` date every second with the current // date as long as the component is mounted. useEffect(() => { const timer = setInterval(() => { - setNow((now) => { - if (now > target) { - clearInterval(timer); - } - return new Date(); - }); + if (now.value > target) { + clearInterval(timer); + } + now.value = new Date(); }, 1000); return () => clearInterval(timer); }, [props.target]); diff --git a/init.ts b/init.ts index 1d892653..2c382e27 100644 --- a/init.ts +++ b/init.ts @@ -112,9 +112,11 @@ await Deno.writeTextFile( ); const ROUTES_INDEX_TSX = `import { Head } from "$fresh/runtime.ts"; +import { useSignal } from "@preact/signals"; import Counter from "../islands/Counter.tsx"; export default function Home() { + const count = useSignal(3); return ( <> @@ -132,7 +134,7 @@ export default function Home() { Welcome to \`fresh\`. Try updating this message in the ./routes/index.tsx file, and refresh.

- + ); @@ -164,20 +166,21 @@ await Deno.writeTextFile( COMPONENTS_BUTTON_TSX, ); -const ISLANDS_COUNTER_TSX = `import { useState } from "preact/hooks"; +const ISLANDS_COUNTER_TSX = `import type { Signal } from "@preact/signals"; import { Button } from "../components/Button.tsx"; interface CounterProps { - start: number; + count: Signal; } export default function Counter(props: CounterProps) { - const [count, setCount] = useState(props.start); return ( - {count}

- - + {props.count}

+ + ); } diff --git a/tests/fixture/import_map.json b/tests/fixture/import_map.json index bf3f2f99..b34de5d5 100644 --- a/tests/fixture/import_map.json +++ b/tests/fixture/import_map.json @@ -3,6 +3,8 @@ "$fresh/": "../../", "preact": "https://esm.sh/preact@10.13.1", "preact/": "https://esm.sh/preact@10.13.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.6" + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.6", + "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", + "@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3" } } diff --git a/tests/fixture/islands/Counter.tsx b/tests/fixture/islands/Counter.tsx index 953a2a60..5f015b1e 100644 --- a/tests/fixture/islands/Counter.tsx +++ b/tests/fixture/islands/Counter.tsx @@ -1,19 +1,18 @@ -import { useState } from "preact/hooks"; +import type { Signal } from "@preact/signals"; import { IS_BROWSER } from "$fresh/runtime.ts"; interface CounterProps { - start: number; + count: Signal; id: string; } export default function Counter(props: CounterProps) { - const [count, setCount] = useState(props.start); return (
-

{count}

+

{props.count}