#Listen for events in .astro page from solid.js UI component?
4 messages · Page 1 of 1 (latest)
Hi @buoyant garden. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro does not support listening for events directly from a Solid.js component in an Astro page. This is because Astro components do not re-render, and thus cannot subscribe to store changes from a .astro file. This is mentioned in a Github issue (https://github.com/withastro/astro/issues/8577) and the Astro documentation (https://docs.astro.build/en/core-concepts/sharing-state/).
However, you can add interactivity to your Astro components using standard HTML <script> tags. This allows you to send JavaScript to run in the browser and add functionality to your Astro components. For example, you can use addEventListener in a <script> tag to handle user interactions. Here's an example from the Astro documentation (https://docs.astro.build/en/guides/client-side-scripts/#handle-onclick-and-other-events):
<button class="alert">Click me!</button>
<script>
// Find all buttons with the `alert` class on the page.
const buttons = document.querySelectorAll('button.alert');
// Handle clicks on each button.
buttons.forEach((button) => {
button.addEventListener('click', () => {
alert('Button was clicked!');
});
});
</script>
In this example, an event listener is added to each button with the alert class. When a button is clicked, an alert is displayed.
If you want to pass data between Astro components and SolidJS/other component types, you might need to use a state management library or a similar solution. However, the specifics of this would depend on your exact use case and the structure of your project.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@buoyant garden kapa.ai is still learning and improving, please let us know how it did by reacting below