Astro islands communicate most simply through browser custom events: one island calls window.dispatchEvent(new CustomEvent('cart:add', { detail })), and any other island, in any framework, listens with window.addEventListener('cart:add', handler). No library is needed, it works across React, Svelte, Vue and plain scripts, and it suits one-off messages like “item added” or “open the menu”.
Key Takeaways
- Custom events are the browser’s built-in message bus; every island can use them.
- Namespace event names (
cart:add) and type thedetailpayload.- Remove listeners when a framework component unmounts.
- An island that hasn’t hydrated yet misses events; use a store for state that must be read later.
Dispatch from one island
// src/lib/events.ts
export type AppEvents = {
'cart:add': { id: string; name: string };
'menu:toggle': { open: boolean };
};
export function emit<K extends keyof AppEvents>(name: K, detail: AppEvents[K]) {
window.dispatchEvent(new CustomEvent(name, { detail }));
}
export function on<K extends keyof AppEvents>(name: K, fn: (detail: AppEvents[K]) => void) {
const handler = (e: Event) => fn((e as CustomEvent<AppEvents[K]>).detail);
window.addEventListener(name, handler);
return () => window.removeEventListener(name, handler);
}
A React island sends an event:
// src/components/AddButton.tsx
import { emit } from '../lib/events';
export default function AddButton({ id, name }: { id: string; name: string }) {
return <button type="button" onClick={() => emit('cart:add', { id, name })}>Add</button>;
}
Listen in another island
A Svelte island reacts, and cleans up when destroyed:
<!-- src/components/CartCount.svelte -->
<script lang="ts">
import { onMount } from 'svelte';
import { on } from '../lib/events';
let count = 0;
onMount(() => on('cart:add', () => (count += 1)));
</script>
<span>Cart: {count}</span>
onMount runs the returned function on destroy, so the listener is removed. In React, return the cleanup from useEffect:
// src/components/Announcer.tsx
import { useEffect, useState } from 'react';
import { on } from '../lib/events';
export default function Announcer() {
const [msg, setMsg] = useState('');
useEffect(() => on('cart:add', ({ name }) => setMsg(`${name} added`)), []);
return <p role="status">{msg}</p>;
}
A plain <script> in an .astro component can listen the same way, which makes events the easiest way for a static header to react to an island.
The hydration timing trap
Events are fire-and-forget. If the listener’s island uses client:visible and is still off-screen, it hasn’t hydrated, so it never hears the event. The count it shows when it finally hydrates starts from zero.
Two fixes:
- Hydrate the listener earlier with
client:loadorclient:idleif it must hear everything. - Store the state, signal the change. Keep the cart in a Nano Store and use events only for transient effects like a toast. A late island reads the store’s current value when it hydrates.
The rule of thumb: events for “something happened”, stores for “what is true now”.
window, document or an element
windowis the global bus. Use it for app-wide events.- An element keeps events local. Dispatch with
bubbles: truefrom inside a component and listen on a wrapper, which is useful for repeated widgets on one page. documentis where Astro’s own view transition events fire (astro:page-load,astro:after-swap). Keep your events off those names.
When events are the right choice
| Need | Use |
|---|---|
| A one-off signal (added, opened, saved) | Custom event |
| A value several islands display | Nano Store |
| Data from the server at render time | Props |
| Showing a message from anywhere | Custom event plus a toast component |
The islands architecture guide explains why islands can’t share framework context, and which client directive to use helps with the timing issue above. The folder structure docs show where shared lib/ code lives, as in every one of our Astro themes.
FAQ
How do Astro islands communicate?
Through browser APIs or shared modules. Custom events on window send messages; a shared store such as Nano Stores shares state. Islands can’t share React context or framework-specific providers.
Do custom events work between React and Svelte islands?
Yes. Custom events are a browser feature, so any framework, or plain JavaScript, can dispatch and listen to them.
Why doesn’t my island receive the event?
It probably hadn’t hydrated when the event fired, often because of client:visible. Hydrate it earlier, or keep the state in a store that the island reads when it starts.