Skip to content

Instantly share code, notes, and snippets.

@sergiodxa
Last active October 12, 2025 00:26
Show Gist options
  • Save sergiodxa/e899adcad20c925fe0369b6e3556e8ed to your computer and use it in GitHub Desktop.
Save sergiodxa/e899adcad20c925fe0369b6e3556e8ed to your computer and use it in GitHub Desktop.
A state helper that simulates useState for Remix v3
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>
);
}
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