---
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
```
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 (
);
}
```
Now we can add the islands to our site by doing the following:
```tsx
```
What happens as a result? The `cart` signal is shared across the two `AddToCart`
islands _and_ the `Cart` island.