Share State Between Astro Components
When building an Astro website, you may need to share state across components. Astro recommends the use of Nano Stores for shared client storage.
Recipe
Section titled Recipe- Install Nano Stores:
npm install nanostorespnpm add nanostoresyarn add nanostores- Create a store. In this example, the store tracks whether a dialog is open or not:
import { atom } from 'nanostores';
export const isOpen = atom(false);- Import and use the store in a <script>tag in the components that will share state.
The following Button and Dialog components each use the shared isOpen state to control whether a particular <div> is hidden or displayed:
<button id="openDialog">Open</button>
<script>  import { isOpen } from '../store.js';
  // Set the store to true when the button is clicked  function openDialog() {    isOpen.set(true);  }
  // Add an event listener to the button  document.getElementById('openDialog').addEventListener('click', openDialog);</script><div id="dialog" style="display: hidden">Hello world!</div>
<script>  import { isOpen } from '../store.js';
  // Listen to changes in the store, and show/hide the dialog accordingly  isOpen.subscribe(open => {    if (open) {      document.getElementById('dialog').style.display = 'block';    } else {      document.getElementById('dialog').style.display = 'none';    }  })
  document.getElementById('openDialog').addEventListener('click', openDialog);</script>Resources
Section titled ResourcesMore recipes
- 
	
	Share State Between IslandsLearn how to share state across framework components with Nano Stores. 
- 
	
	Add an RSS feedAdd an RSS feed to your Astro site to let users subscribe to your content. 
- 
	
	Installing a Vite or Rollup pluginLearn how you can import YAML data by adding a Rollup plugin to your project. 
- 
	
	Build a custom image componentLearn how to build a custom image component that supports media queries using the getImage function 
- 
	
	Build Forms With API RoutesLearn how to use JavaScript to send form submissions to an API Route 
- 
	
	Build HTML Forms in Astro PagesLearn how to build HTML forms and handle submissions in your frontmatter 
- 
	
	Use Bun with AstroLearn how to use Bun with your Astro site. 
- 
	
	Call endpoints from the serverLearn how to call endpoints from the server in Astro. 
- 
	
	Verify a CaptchaLearn how to create an API route and fetch it from the client. 
- 
	
	Build your Astro Site with DockerLearn how to build your Astro site using Docker. 
- 
	
	Add icons to external linksLearn how to install a rehype plugin to add icons to external links in your Markdown files 
- 
	
	Add i18n featuresUse dynamic routing and content collections to add internationalization support to your Astro site. 
- 
	
	Add Last Modified TimeBuild a remark plugin to add the last modified time to your Markdown and MDX. 
- 
	
	Add Reading TimeBuild a remark plugin to add reading time to your Markdown or MDX files. 
- 
	
	Share State Between Astro ComponentsLearn how to share state across Astro components with Nano Stores. 
- 
	
	Using streaming to improve page performanceLearn how to use streaming to improve page performance. 
- 
	
	Style Rendered Markdown with Tailwind TypographyLearn how to use @tailwind/typography to style your rendered Markdown