---
description: |
When you need to have state shared between islands, this page provides a few recipes.
---
## 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.
## Sharing State Across Independent Islands
When islands are not rendered as siblings (e.g. one in a sidebar and one in the
main content), you can share state by creating a signal in a parent component
and passing it as a prop to each island.
```tsx islands/AddToCart.tsx
import { type Signal } from "@preact/signals";
import { Button } from "../components/Button.tsx";
interface AddToCartProps {
cart: Signal;
product: string;
}
export default function AddToCart(props: AddToCartProps) {
const { cart, product } = props;
return (
);
}
```
```tsx islands/Cart.tsx
import { type Signal } from "@preact/signals";
import { Button } from "../components/Button.tsx";
import * as icons from "../components/Icons.tsx";
interface CartProps {
cart: Signal;
}
export default function Cart(props: CartProps) {
const { cart } = props;
return (
);
}
```
Then wire them together from a route, passing the same signal to both:
```tsx routes/cart.tsx
import { useSignal } from "@preact/signals";
import AddToCart from "../islands/AddToCart.tsx";
import Cart from "../islands/Cart.tsx";
import { define } from "../utils.ts";
export default define.page(function CartPage() {
const cart = useSignal([]);
return (
);
});
```
The `cart` signal is created per-render (not at module level), so each request
gets its own independent cart. Fresh [serializes](/docs/advanced/serialization)
the signal and passes it to both islands, keeping them in sync on the client.
> [!CAUTION]
> Avoid creating signals at the module level (e.g.
> `export const cart =
> signal([])` in a utility file). Module-level state is
> shared across all requests on the server, which means different users would
> see the same cart. Always create signals inside components or handlers.