#What's the standard approach to build a component which has some dynamic parts?
1 messages · Page 1 of 1 (latest)
Astro provides a flexible way to build components that can be both static and dynamic. Here's a general approach based on the information provided in the extracts:
-
Astro Component: Start by creating your
ItemButtonas an Astro component. Astro components are HTML-only templating components with no client-side runtime, which makes them ideal for static content. You can spot an Astro component by its file extension:.astro(source (https://docs.astro.build/en/basics/astro-components#_top)). -
Dynamic Parts: For the dynamic parts of your component, you can use JavaScript expressions within the component template. These expressions can use data and values defined in the component script to produce dynamically-created HTML (source (https://docs.astro.build/en/basics/astro-components#the-component-template)).
-
Client-side Interactivity: When your Astro component needs client-side interactivity, you can add standard HTML
<script>tags or UI Framework components. For example, you can use a<script>tag to check and set the current theme fromlocalStorageand toggle the theme when the icon is clicked (source (https://docs.astro.build/en/tutorial/6-islands/2#add-client-side-interactivity)). -
State Management: For sharing state across components, Astro recommends the use of Nano Stores for shared client storage. This can be useful for managing your theme state globally (source (https://docs.astro.build/en/recipes/sharing-state#_top)).
- Framework Components: If you find that you need more dynamic behavior than what Astro components can provide, you can use a framework component from a JavaScript framework like Vue. You can import and use these components in your Astro pages, layouts, and components just like Astro components (source (https://docs.astro.build/en/guides/framework-components#using-framework-components)).