Last active
December 15, 2023 20:41
-
-
Save czottmann/971402a89775b261176c2fd217e82110 to your computer and use it in GitHub Desktop.
Deno + Statery example
This file contains 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
/** @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> | |
); | |
} |
This file contains 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
/** @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>; | |
} |
This file contains 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
/** @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> | |
); | |
} |
This file contains 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
// ./stores/whatever_store.ts | |
import { | |
makeStore, | |
useStore, | |
} from "https://esm.sh/[email protected]?alias=react:preact/compat&[email protected]"; | |
/* 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