From 7999d500f8dd29baccf15348cf8b578cf84dfc8c Mon Sep 17 00:00:00 2001 From: Zhi Date: Fri, 12 Sep 2025 23:16:30 +0800 Subject: [PATCH] docs: fix missing files on "sharing state with islands" page (#3377) close #3376 --- .../examples/sharing-state-between-islands.md | 189 ++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 docs/latest/examples/sharing-state-between-islands.md diff --git a/docs/latest/examples/sharing-state-between-islands.md b/docs/latest/examples/sharing-state-between-islands.md new file mode 100644 index 00000000..290236d8 --- /dev/null +++ b/docs/latest/examples/sharing-state-between-islands.md @@ -0,0 +1,189 @@ +--- +description: | + When you need to have state shared between islands, this page provides a few recipes. +--- + +All of this content is lifted from this great +[example](https://fresh-with-signals.deno.dev/) by Luca. The source can be found +[here](https://github.com/lucacasonato/fresh-with-signals). + +## Multiple Sibling Islands with Independent State + +Imagine we have `Counter.tsx` like this: + +```tsx islands/Counter.tsx +import { useSignal } from "@preact/signals"; +import { Button } from "../components/Button.tsx"; + +interface CounterProps { + start: number; +} + +// This island is used to display a counter and increment/decrement it. The +// state for the counter is stored locally in this island. +export default function Counter(props: CounterProps) { + const count = useSignal(props.start); + return ( +
+

{count}

+ + +
+ ); +} +``` + +Note how `useSignal` is within the `Counter` component. Then if we instantiate +some counters like this... + +```tsx routes/index.tsx + + +``` + +they'll keep track of their own independent state. Not much sharing going on +here, yet. + +## Multiple Sibling Islands with Shared State + +But we can switch things up by looking at a `SynchronizedSlider.tsx` like this: + +```tsx islands/SynchronizedSlider.tsx +import { Signal } from "@preact/signals"; + +interface SliderProps { + slider: Signal; +} + +// This island displays a slider with a value equal to the `slider` signal's +// value. When the slider is moved, the `slider` signal is updated. +export default function SynchronizedSlider(props: SliderProps) { + return ( + (props.slider.value = Number(e.currentTarget.value))} + /> + ); +} +``` + +Now if we were to do the following... + +```tsx routes/index.tsx +export default function Home() { + const sliderSignal = useSignal(50); + return ( +
+ + + +
+ ); +} +``` + +they would all use the same value. + +## Independent Islands + +We can also create a `signal` in a utility file and export it for consumption +across multiple places. + +```ts utils/cart.ts +import { signal } from "@preact/signals"; + +export const cart = signal([]); +``` + +```tsx islands/AddToCart.tsx +import { Button } from "../components/Button.tsx"; +import { cart } from "../utils/cart.ts"; + +interface AddToCartProps { + product: string; +} + +// This island is used to add a product to the cart state. +export default function AddToCart(props: AddToCartProps) { + return ( + + ); +} +``` + +```tsx islands/Cart.tsx +import { Button } from "../components/Button.tsx"; +import { cart } from "../utils/cart.ts"; +import * as icons from "../components/Icons.tsx"; + +// This island is used to display the cart contents and remove items from it. +export default function Cart() { + return ( +

+ Cart +

+ +
    + {cart.value.length === 0 && ( +
  • +
    + +
    + Your cart is empty. +
    +
    +
  • + )} + {cart.value.map((product, index) => ( + + ))} +
+ ); +} + +interface CartItemProps { + product: string; + index: number; +} + +function CartItem(props: CartItemProps) { + const remove = () => { + const newCart = [...cart.value]; + newCart.splice(props.index, 1); + cart.value = newCart; + }; + + return ( +
  • + +
    + {props.product} +
    + +
  • + ); +} +``` + +Now we can add the islands to our site by doing the following: + +```tsx routes/cart.tsx + + + +``` + +What happens as a result? The `cart` signal is shared across the two `AddToCart` +islands _and_ the `Cart` island.