Skip to content

Instantly share code, notes, and snippets.

@czottmann
Last active December 15, 2023 20:41
Show Gist options
  • Save czottmann/971402a89775b261176c2fd217e82110 to your computer and use it in GitHub Desktop.
Save czottmann/971402a89775b261176c2fd217e82110 to your computer and use it in GitHub Desktop.
Deno + Statery example
/** @jsx h */
import { h } from "preact";
import { incrementCount, state } from "../stores/whatever_store.ts";
export default function StoreUsingComponent1() {
const { counter } = state();
return (
<p>
<button onClick={incrementCount}>
Increment number (clicked {counter} times)
</button>
</p>
);
}
/** @jsx h */
import { h } from "preact";
import { state } from "../stores/whatever_store.ts";
export default function StoreUsingComponent1() {
const { counter } = state();
return <p>Current counter value: {counter}</p>;
}
/** @jsx h */
import { h } from "preact";
import StoreUsingComponent1 from "../islands/StoreUsingComponent1.tsx";
import StoreUsingComponent2 from "../islands/StoreUsingComponent2.tsx";
export default function Demo() {
return (
<div>
<StoreUsingComponent1 />
<StoreUsingComponent2 />
</div>
);
}
// ./stores/whatever_store.ts
import {
makeStore,
useStore,
} from "https://esm.sh/statery@0.5.4?alias=react:preact/compat&deps=preact@10.8.2";
/* Make a store */
const store = makeStore({ counter: 0 });
/* Write some code that updates it */
export const incrementCount = () =>
store.set((state) => ({
counter: state.counter + 1,
}));
/**
* A wee bit of abstraction so my components don't need to import `useState`
* from Statery
*/
export const state = () => useStore(store);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment