Last active
October 12, 2025 00:26
-
-
Save sergiodxa/e899adcad20c925fe0369b6e3556e8ed to your computer and use it in GitHub Desktop.
A state helper that simulates useState for Remix v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Remix } from "@remix-run/dom"; | |
import { press } from "@remix-run/events/press"; | |
import { state } from "./state"; | |
export function Counter(this: Remix.Handle, { initial }: { initial?: number }) { | |
let [count, setCount] = state(this, initial ?? 0); | |
return () => ( | |
<button type="button" on={[press(() => setCount((c) => c + 1))]}> | |
Count: <span>{count()}</span> | |
</button> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { Remix } from "@remix-run/dom"; | |
export function state<T>(handle: Remix.Handle, initial: T) { | |
let value = initial; | |
return [ | |
() => value, | |
(newValueOrFn: T | ((current: T) => T), renderFn?: Remix.Task) => { | |
if (typeof newValueOrFn === "function") { | |
value = (newValueOrFn as (current: T) => T)(value); | |
} else { | |
value = newValueOrFn; | |
} | |
handle.update(renderFn); | |
}, | |
] as const; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment