refactor: replace useState() with useSignal() (#1234)
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
export default function RootFragment() {
|
||||
const [shown, setShown] = useState(false);
|
||||
const shown = useSignal(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
Hello
|
||||
<div onClick={() => setShown(true)} id="root-fragment-click-me">
|
||||
<div onClick={() => shown.value = true} id="root-fragment-click-me">
|
||||
World
|
||||
</div>
|
||||
{shown && <div>I'm rendered now</div>}
|
||||
{shown.value && <div>I'm rendered now</div>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import { useSignal } from "@preact/signals";
|
||||
|
||||
export default function RootFragmentWithConditionalFirst() {
|
||||
const [shown, setShown] = useState(false);
|
||||
const shown = useSignal(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
{shown && <div>I'm rendered on top</div>}
|
||||
{shown.value && <div>I'm rendered on top</div>}
|
||||
Hello
|
||||
<div
|
||||
onClick={() => setShown(true)}
|
||||
onClick={() => shown.value = true}
|
||||
id="root-fragment-conditional-first-click-me"
|
||||
>
|
||||
World
|
||||
|
||||
@@ -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<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
import { useState } from "preact/hooks";
|
||||
import type { Signal } from "@preact/signals";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
|
||||
interface KebabCaseFileNameTestProps {
|
||||
start: number;
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function KebabCaseFileNameTest(
|
||||
props: KebabCaseFileNameTestProps,
|
||||
) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import Counter from "../../islands/Counter.tsx";
|
||||
import FolderCounter from "../../islands/folder/Counter.tsx";
|
||||
import KebabCaseFileNameTest from "../../islands/kebab-case-counter-test.tsx";
|
||||
@@ -6,10 +7,13 @@ import Test from "../../islands/Test.tsx";
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<Counter id="counter1" start={3} />
|
||||
<Counter id="counter2" start={10} />
|
||||
<FolderCounter id="folder-counter" start={3} />
|
||||
<KebabCaseFileNameTest id="kebab-case-file-counter" start={5} />
|
||||
<Counter id="counter1" count={useSignal(3)} />
|
||||
<Counter id="counter2" count={useSignal(10)} />
|
||||
<FolderCounter id="folder-counter" count={useSignal(3)} />
|
||||
<KebabCaseFileNameTest
|
||||
id="kebab-case-file-counter"
|
||||
count={useSignal(5)}
|
||||
/>
|
||||
<Test message="" />
|
||||
<Test message={`</script><script>alert('test')</script>`} />
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user