Share State Between Islands
When building an Astro website with islands architecture / partial hydration, you may have run into this problem: I want to share state between my components.
UI frameworks like React or Vue may encourage “context” providers for other components to consume. But when partially hydrating components within Astro or Markdown, you can’t use these context wrappers.
Astro recommends a different solution for shared client-side storage: Nano Stores.
![](/houston_chef.webp)
Why Nano Stores?
Section titled Why Nano Stores?The Nano Stores library allows you to author stores that any component can interact with. We recommend Nano Stores because:
- They’re lightweight. Nano Stores ship the bare minimum JS you’ll need (less than 1 KB) with zero dependencies.
- They’re framework-agnostic. This means sharing state between frameworks will be seamless! Astro is built on flexibility, so we love solutions that offer a similar developer experience no matter your preference.
Still, there are a number of alternatives you can explore. These include:
- Svelte’s built-in stores
- Solid signals outside of a component context
- Vue’s reactivity API
- Sending custom browser events between components
Installing Nano Stores
Section titled Installing Nano StoresTo get started, install Nano Stores alongside their helper package for your favorite UI framework:
npm install nanostores @nanostores/preact
npm install nanostores @nanostores/react
npm install nanostores @nanostores/solid
npm install nanostores @nanostores/vue
npm install nanostores @nanostores/lit
npm install nanostores
You can jump into the Nano Stores usage guide from here, or follow along with our example below!
Usage example - ecommerce cart flyout
Section titled Usage example - ecommerce cart flyoutLet’s say we’re building a simple ecommerce interface with three interactive elements:
- An “add to cart” submission form
- A cart flyout to display those added items
- A cart flyout toggle
Try the completed example on your machine or online via StackBlitz.
Your base Astro file may look like this:
---import CartFlyoutToggle from '../components/CartFlyoutToggle';import CartFlyout from '../components/CartFlyout';import AddToCartForm from '../components/AddToCartForm';---
<!DOCTYPE html><html lang="en"><head>...</head><body> <header> <nav> <a href="/">Astro storefront</a> <CartFlyoutToggle client:load /> </nav> </header> <main> <AddToCartForm client:load> <!-- ... --> </AddToCartForm> </main> <CartFlyout client:load /></body></html>
Using “atoms”
Section titled Using “atoms”Let’s start by opening our CartFlyout
whenever CartFlyoutToggle
is clicked.
First, create a new JS or TS file to contain our store. We’ll use an “atom” for this:
import { atom } from 'nanostores';
export const isCartOpen = atom(false);
Now, we can import this store into any file that needs to read or write. We’ll start by wiring up our CartFlyoutToggle
:
import { useStore } from '@nanostores/preact';import { isCartOpen } from '../cartStore';
export default function CartButton() { // read the store value with the `useStore` hook const $isCartOpen = useStore(isCartOpen); // write to the imported store using `.set` return ( <button onClick={() => isCartOpen.set(!$isCartOpen)}>Cart</button> )}
import { useStore } from '@nanostores/react';import { isCartOpen } from '../cartStore';
export default function CartButton() { // read the store value with the `useStore` hook const $isCartOpen = useStore(isCartOpen); // write to the imported store using `.set` return ( <button onClick={() => isCartOpen.set(!$isCartOpen)}>Cart</button> )}
import { useStore } from '@nanostores/solid';import { isCartOpen } from '../cartStore';
export default function CartButton() { // read the store value with the `useStore` hook const $isCartOpen = useStore(isCartOpen); // write to the imported store using `.set` return ( <button onClick={() => isCartOpen.set(!$isCartOpen())}>Cart</button> )}
<script> import { isCartOpen } from '../cartStore';</script>
<!--use "$" to read the store value--><button on:click={() => isCartOpen.set(!$isCartOpen)}>Cart</button>
<template> <!--write to the imported store using `.set`--> <button @click="isCartOpen.set(!$isCartOpen)">Cart</button></template>
<script setup> import { isCartOpen } from '../cartStore'; import { useStore } from '@nanostores/vue';
// read the store value with the `useStore` hook const $isCartOpen = useStore(isCartOpen);</script>
import { LitElement, html } from 'lit';import { isCartOpen } from '../cartStore';
export class CartFlyoutToggle extends LitElement { handleClick() { isCartOpen.set(!isCartOpen.get()); }
render() { return html` <button @click="${this.handleClick}">Cart</button> `; }}
customElements.define('cart-flyout-toggle', CartFlyoutToggle);
Then, we can read isCartOpen
from our CartFlyout
component:
import { useStore } from '@nanostores/preact';import { isCartOpen } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen);
return $isCartOpen ? <aside>...</aside> : null;}
import { useStore } from '@nanostores/react';import { isCartOpen } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen);
return $isCartOpen ? <aside>...</aside> : null;}
import { useStore } from '@nanostores/solid';import { isCartOpen } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen);
return $isCartOpen() ? <aside>...</aside> : null;}
<script> import { isCartOpen } from '../cartStore';</script>
{#if $isCartOpen}<aside>...</aside>{/if}
<template> <aside v-if="$isCartOpen">...</aside></template>
<script setup> import { isCartOpen } from '../cartStore'; import { useStore } from '@nanostores/vue';
const $isCartOpen = useStore(isCartOpen);</script>
import { isCartOpen } from '../cartStore';import { LitElement, html } from 'lit';import { StoreController } from '@nanostores/lit';
export class CartFlyout extends LitElement { private cartOpen = new StoreController(this, isCartOpen);
render() { return this.cartOpen.value ? html`<aside>...</aside>` : null; }}
customElements.define('cart-flyout', CartFlyout);
Using “maps”
Section titled Using “maps”Now, let’s keep track of the items inside your cart. To avoid duplicates and keep track of “quantity,” we can store your cart as an object with the item’s ID as a key. We’ll use a Map for this.
Let’s add a cartItem
store to our cartStore.js
from earlier. You can also switch to a TypeScript file to define the shape if you’re so inclined.
import { atom, map } from 'nanostores';
export const isCartOpen = atom(false);
/** * @typedef {Object} CartItem * @property {string} id * @property {string} name * @property {string} imageSrc * @property {number} quantity */
/** @type {import('nanostores').MapStore<Record<string, CartItem>>} */export const cartItems = map({});
import { atom, map } from 'nanostores';
export const isCartOpen = atom(false);
export type CartItem = { id: string; name: string; imageSrc: string; quantity: number;}
export const cartItems = map<Record<string, CartItem>>({});
Now, let’s export an addCartItem
helper for our components to use.
- If that item doesn’t exist in your cart, add the item with a starting quantity of 1.
- If that item does already exist, bump the quantity by 1.
...export function addCartItem({ id, name, imageSrc }) { const existingEntry = cartItems.get()[id]; if (existingEntry) { cartItems.setKey(id, { ...existingEntry, quantity: existingEntry.quantity + 1, }) } else { cartItems.setKey( id, { id, name, imageSrc, quantity: 1 } ); }}
...type ItemDisplayInfo = Pick<CartItem, 'id' | 'name' | 'imageSrc'>;export function addCartItem({ id, name, imageSrc }: ItemDisplayInfo) { const existingEntry = cartItems.get()[id]; if (existingEntry) { cartItems.setKey(id, { ...existingEntry, quantity: existingEntry.quantity + 1, }); } else { cartItems.setKey( id, { id, name, imageSrc, quantity: 1 } ); }}
With our store in place, we can call this function inside our AddToCartForm
whenever that form is submitted. We’ll also open the cart flyout so you can see a full cart summary.
import { addCartItem, isCartOpen } from '../cartStore';
export default function AddToCartForm({ children }) { // we'll hardcode the item info for simplicity! const hardcodedItemInfo = { id: 'astronaut-figurine', name: 'Astronaut Figurine', imageSrc: '/images/astronaut-figurine.png', }
function addToCart(e) { e.preventDefault(); isCartOpen.set(true); addCartItem(hardcodedItemInfo); }
return ( <form onSubmit={addToCart}> {children} </form> )}
import { addCartItem, isCartOpen } from '../cartStore';
export default function AddToCartForm({ children }) { // we'll hardcode the item info for simplicity! const hardcodedItemInfo = { id: 'astronaut-figurine', name: 'Astronaut Figurine', imageSrc: '/images/astronaut-figurine.png', }
function addToCart(e) { e.preventDefault(); isCartOpen.set(true); addCartItem(hardcodedItemInfo); }
return ( <form onSubmit={addToCart}> {children} </form> )}
import { addCartItem, isCartOpen } from '../cartStore';
export default function AddToCartForm({ children }) { // we'll hardcode the item info for simplicity! const hardcodedItemInfo = { id: 'astronaut-figurine', name: 'Astronaut Figurine', imageSrc: '/images/astronaut-figurine.png', }
function addToCart(e) { e.preventDefault(); isCartOpen.set(true); addCartItem(hardcodedItemInfo); }
return ( <form onSubmit={addToCart}> {children} </form> )}
<form on:submit|preventDefault={addToCart}> <slot></slot></form>
<script> import { addCartItem, isCartOpen } from '../cartStore';
// we'll hardcode the item info for simplicity! const hardcodedItemInfo = { id: 'astronaut-figurine', name: 'Astronaut Figurine', imageSrc: '/images/astronaut-figurine.png', }
function addToCart() { isCartOpen.set(true); addCartItem(hardcodedItemInfo); }</script>
<template> <form @submit="addToCart"> <slot></slot> </form></template>
<script setup> import { addCartItem, isCartOpen } from '../cartStore';
// we'll hardcode the item info for simplicity! const hardcodedItemInfo = { id: 'astronaut-figurine', name: 'Astronaut Figurine', imageSrc: '/images/astronaut-figurine.png', }
function addToCart(e) { e.preventDefault(); isCartOpen.set(true); addCartItem(hardcodedItemInfo); }</script>
import { LitElement, html } from 'lit';import { isCartOpen, addCartItem } from '../cartStore';
export class AddToCartForm extends LitElement { static get properties() { return { item: { type: Object }, }; }
constructor() { super(); this.item = {}; }
addToCart(e) { e.preventDefault(); isCartOpen.set(true); addCartItem(this.item); }
render() { return html` <form @submit="${this.addToCart}"> <slot></slot> </form> `; }}customElements.define('add-to-cart-form', AddToCartForm);
Finally, we’ll render those cart items inside our CartFlyout
:
import { useStore } from '@nanostores/preact';import { isCartOpen, cartItems } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen); const $cartItems = useStore(cartItems);
return $isCartOpen ? ( <aside> {Object.values($cartItems).length ? ( <ul> {Object.values($cartItems).map(cartItem => ( <li> <img src={cartItem.imageSrc} alt={cartItem.name} /> <h3>{cartItem.name}</h3> <p>Quantity: {cartItem.quantity}</p> </li> ))} </ul> ) : <p>Your cart is empty!</p>} </aside> ) : null;}
import { useStore } from '@nanostores/react';import { isCartOpen, cartItems } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen); const $cartItems = useStore(cartItems);
return $isCartOpen ? ( <aside> {Object.values($cartItems).length ? ( <ul> {Object.values($cartItems).map(cartItem => ( <li> <img src={cartItem.imageSrc} alt={cartItem.name} /> <h3>{cartItem.name}</h3> <p>Quantity: {cartItem.quantity}</p> </li> ))} </ul> ) : <p>Your cart is empty!</p>} </aside> ) : null;}
import { useStore } from '@nanostores/solid';import { isCartOpen, cartItems } from '../cartStore';
export default function CartFlyout() { const $isCartOpen = useStore(isCartOpen); const $cartItems = useStore(cartItems);
return $isCartOpen() ? ( <aside> {Object.values($cartItems()).length ? ( <ul> {Object.values($cartItems()).map(cartItem => ( <li> <img src={cartItem.imageSrc} alt={cartItem.name} /> <h3>{cartItem.name}</h3> <p>Quantity: {cartItem.quantity}</p> </li> ))} </ul> ) : <p>Your cart is empty!</p>} </aside> ) : null;}
<script> import { isCartOpen, cartItems } from '../cartStore';</script>
{#if $isCartOpen} {#if Object.values($cartItems).length} <aside> {#each Object.values($cartItems) as cartItem} <li> <img src={cartItem.imageSrc} alt={cartItem.name} /> <h3>{cartItem.name}</h3> <p>Quantity: {cartItem.quantity}</p> </li> {/each} </aside> {#else} <p>Your cart is empty!</p> {/if}{/if}
<template> <aside v-if="$isCartOpen"> <ul v-if="Object.values($cartItems).length"> <li v-for="cartItem in Object.values($cartItems)" v-bind:key="cartItem.name"> <img :src=cartItem.imageSrc :alt=cartItem.name /> <h3>{{cartItem.name}}</h3> <p>Quantity: {{cartItem.quantity}}</p> </li> </ul> <p v-else>Your cart is empty!</p> </aside></template>
<script setup> import { cartItems, isCartOpen } from '../cartStore'; import { useStore } from '@nanostores/vue';
const $isCartOpen = useStore(isCartOpen); const $cartItems = useStore(cartItems);</script>
import { LitElement, html } from 'lit';import { isCartOpen, cartItems } from '../cartStore';import { StoreController } from '@nanostores/lit';
export class CartFlyoutLit extends LitElement { private cartOpen = new StoreController(this, isCartOpen); private getCartItems = new StoreController(this, cartItems);
renderCartItem(cartItem) { return html` <li> <img src="${cartItem.imageSrc}" alt="${cartItem.name}" /> <h3>${cartItem.name}</h3> <p>Quantity: ${cartItem.quantity}</p> </li> `; }
render() { return this.cartOpen.value ? html` <aside> ${ Object.values(this.getCartItems.value).length ? html` <ul> ${Object.values(this.getCartItems.value).map((cartItem) => this.renderCartItem(cartItem) )} </ul> ` : html`<p>Your cart is empty!</p>` } </aside> ` : null; }}
customElements.define('cart-flyout', CartFlyoutLit);
Now, you should have a fully interactive ecommerce example with the smallest JS bundle in the galaxy 🚀
Try the completed example on your machine or online via StackBlitz!
More recipes
-
Share State Between Islands
Learn how to share state across framework components with Nano Stores.
-
Add an RSS feed
Add an RSS feed to your Astro site to let users subscribe to your content.
-
Installing a Vite or Rollup plugin
Learn how you can import YAML data by adding a Rollup plugin to your project.
-
Build a custom image component
Learn how to build a custom image component that supports media queries using the getImage function
-
Build Forms With API Routes
Learn how to use JavaScript to send form submissions to an API Route
-
Build HTML Forms in Astro Pages
Learn how to build HTML forms and handle submissions in your frontmatter
-
Use Bun with Astro
Learn how to use Bun with your Astro site.
-
Call endpoints from the server
Learn how to call endpoints from the server in Astro.
-
Verify a Captcha
Learn how to create an API route and fetch it from the client.
-
Build your Astro Site with Docker
Learn how to build your Astro site using Docker.
-
Dynamically Import Images
Learn how to dynamically import images using Vite's import.meta.glob function
-
Add icons to external links
Learn how to install a rehype plugin to add icons to external links in your Markdown files
-
Add i18n features
Use dynamic routing and content collections to add internationalization support to your Astro site.
-
Add Last Modified Time
Build a remark plugin to add the last modified time to your Markdown and MDX.
-
Add Reading Time
Build a remark plugin to add reading time to your Markdown or MDX files.
-
Share State Between Astro Components
Learn how to share state across Astro components with Nano Stores.
-
Using streaming to improve page performance
Learn how to use streaming to improve page performance.
-
Style Rendered Markdown with Tailwind Typography
Learn how to use @tailwind/typography to style your rendered Markdown