refactor: replace useState() with useSignal() (#1234)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user