From 4de93945472b5ad00fa4d0a16e5f7ffc44231582 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Wed, 28 Jun 2023 22:17:16 +0200 Subject: [PATCH] docs: add "Sharing state between islands" example (#1311) --- docs/examples/index.md | 1 + .../examples/sharing-state-between-islands.md | 195 ++++++++++++++++++ docs/toc.json | 3 +- 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 docs/examples/sharing-state-between-islands.md diff --git a/docs/examples/index.md b/docs/examples/index.md index 25776adc..9311b756 100644 --- a/docs/examples/index.md +++ b/docs/examples/index.md @@ -18,3 +18,4 @@ like to see here, please open - [Creating a CRUD API](./examples/creating-a-crud-api) - [Handling complex routes](./examples/handling-complex-routes) - [Rendering markdown](./examples/rendering-markdown) +- [Sharing state between islands](./examples/sharing-state-between-islands) diff --git a/docs/examples/sharing-state-between-islands.md b/docs/examples/sharing-state-between-islands.md new file mode 100644 index 00000000..da840f59 --- /dev/null +++ b/docs/examples/sharing-state-between-islands.md @@ -0,0 +1,195 @@ +--- +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: + +```ts +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... + +```ts + + +``` + +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: + +```ts +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... + +```ts +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. + +`./utils/cart.ts` + +```ts +import { signal } from "@preact/signals"; + +export const cart = signal([]); +``` + +`./islands/AddToCart.tsx` + +```ts +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 ( + + ); +} +``` + +`./islands/Cart.tsx` + +```ts +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: + +```ts + + + +``` + +What happens as a result? The `cart` signal is shared across the two `AddToCart` +islands _and_ the `Cart` island. diff --git a/docs/toc.json b/docs/toc.json index 96a931cb..05f5b59d 100644 --- a/docs/toc.json +++ b/docs/toc.json @@ -51,7 +51,8 @@ ["dealing-with-cors", "Dealing with CORS"], ["creating-a-crud-api", "Creating a CRUD API"], ["handling-complex-routes", "Handling complex routes"], - ["rendering-markdown", "Rendering markdown"] + ["rendering-markdown", "Rendering markdown"], + ["sharing-state-between-islands", "Sharing state between islands"] ] } }