To share state between Astro islands, put it in a Nano Store: a tiny, framework-agnostic store module that every island imports. Install nanostores plus the binding for your framework (such as @nanostores/react), create an atom or map in a plain .ts file, and read it with useStore() in each island. Astro Nano Stores are the approach Astro’s own docs recommend, because islands are separate apps with no shared React context.
Key Takeaways
- Islands hydrate independently, so React context or a Svelte store inside one island can’t reach another.
- A Nano Store is a module-level value any island or script can import and subscribe to.
- Use
atomfor single values,mapfor objects andcomputedfor derived values.- Never keep per-visitor data in a store during server rendering; stores are module-level and shared.
Install
# terminal
npm install nanostores @nanostores/react
# or @nanostores/vue, @nanostores/preact, @nanostores/solid
Svelte needs no binding: Nano Stores follow Svelte’s store contract, so $store works directly.
Create the store
// src/stores/cart.ts
import { atom, map, computed } from 'nanostores';
export const isCartOpen = atom(false);
export type CartItem = { id: string; name: string; qty: number; price: number };
export const cartItems = map<Record<string, CartItem>>({});
export const cartCount = computed(cartItems, (items) =>
Object.values(items).reduce((sum, item) => sum + item.qty, 0)
);
export function addToCart(item: Omit<CartItem, 'qty'>) {
const existing = cartItems.get()[item.id];
cartItems.setKey(item.id, { ...item, qty: (existing?.qty ?? 0) + 1 });
}
Keep actions like addToCart next to the store. Islands call the function instead of reaching into the data, which keeps updates consistent.
Use it in islands
A React button in the product grid:
// src/components/AddButton.tsx
import { addToCart } from '../stores/cart';
export default function AddButton({ id, name, price }: { id: string; name: string; price: number }) {
return <button type="button" onClick={() => addToCart({ id, name, price })}>Add to cart</button>;
}
A Svelte badge in the header:
<!-- src/components/CartBadge.svelte -->
<script>
import { cartCount, isCartOpen } from '../stores/cart';
</script>
<button onclick={() => isCartOpen.set(!$isCartOpen)}>
Cart ({$cartCount})
</button>
A React flyout that reads both:
// src/components/CartFlyout.tsx
import { useStore } from '@nanostores/react';
import { isCartOpen, cartItems } from '../stores/cart';
export default function CartFlyout() {
const open = useStore(isCartOpen);
const items = useStore(cartItems);
if (!open) return null;
return <aside><ul>{Object.values(items).map((i) => <li key={i.id}>{i.name} × {i.qty}</li>)}</ul></aside>;
}
Put them on any page with their own directives:
---
// src/pages/shop.astro
import AddButton from '../components/AddButton.tsx';
import CartBadge from '../components/CartBadge.svelte';
import CartFlyout from '../components/CartFlyout.tsx';
---
<CartBadge client:load />
<CartFlyout client:idle />
<AddButton client:visible id="lamp" name="Desk lamp" price={89} />
Three islands, two frameworks, one source of truth. When the button updates cartItems, the badge and flyout re-render.
Plain scripts can join in
Stores work without any framework:
<span id="count">0</span>
<script>
import { cartCount } from '../stores/cart';
cartCount.subscribe((n) => (document.getElementById('count')!.textContent = String(n)));
</script>
Persist across reloads
Stores live in memory, so a full page load resets them. For a cart, use @nanostores/persistent, which syncs to localStorage:
// src/stores/cart.ts
import { persistentAtom } from '@nanostores/persistent';
export const savedCart = persistentAtom<string[]>('cart', [], {
encode: JSON.stringify,
decode: JSON.parse,
});
With <ClientRouter />, module state already survives navigation, because the JavaScript isn’t reloaded; see persisting islands across pages.
The SSR caveat
During server rendering, a store module is shared by every request the server handles. If you write a visitor’s data into a store on the server, the next visitor could see it. Only set stores in the browser, and pass server data to islands as props. Our tip on passing props to islands covers that side.
For one-off notifications rather than shared state, custom events are lighter still. The islands architecture guide explains why islands are isolated, and the folder structure docs show where a stores/ folder fits. Our e-commerce Astro themes are a good base for a cart like this.
FAQ
Can Astro islands share React context?
No. Each island is its own React root, so a context provider in one island doesn’t wrap another. Use a shared store such as Nano Stores, or render everything in one larger island.
Do Nano Stores work across frameworks in Astro?
Yes. The same store can be read from React, Preact, Vue, Svelte, Solid and plain scripts on one page, each with its framework’s binding.
Can I use Zustand or Redux instead?
Zustand’s vanilla store can be imported by several React islands, and that works. Nano Stores are smaller and work across frameworks, which is why Astro’s docs recommend them.