📖 Scenario: You are building a simple Astro page that uses two different framework islands: one in React and one in Vue. Both islands need to share a piece of state: a counter that can be incremented from either island and stays in sync.This is like having two friends in different rooms who want to keep track of the same score on a game. They need a way to share the score so both see the same number.
🎯 Goal: Create an Astro page with a shared count state. Build a React island and a Vue island that both display the count and have a button to increment it. When you click the button in either island, the count updates in both islands.
📋 What You'll Learn
Create a shared state variable called count initialized to 0 in Astro.
Create a React island component that receives count and an increment function as props.
Create a Vue island component that receives count and an increment function as props.
Render both islands in the Astro page and share the count and increment function between them.
💡 Why This Matters
🌍 Real World
Many modern websites use multiple JavaScript frameworks together. Sharing state between these framework islands lets you build rich interactive pages without rewriting everything in one framework.
💼 Career
Understanding how to share state between different UI frameworks is valuable for frontend developers working with Astro or micro-frontend architectures.
Progress0 / 4 steps
1
Set up shared state in Astro
In your Astro page, create a variable called count and set it to 0. Also create a function called increment that increases count by 1.
Astro
Hint
Use let count = 0; to create the count variable. Then write a function increment() that adds 1 to count.
2
Create the React island component
Create a React functional component called CounterReact that accepts count and increment as props. It should render the current count and a button that calls increment when clicked.
Astro
Hint
Write a React function component that takes count and increment as props. Render a paragraph showing the count and a button that calls increment on click.
3
Create the Vue island component
Create a Vue component called CounterVue using the <script setup> syntax. It should accept count and increment as props. Render the current count and a button that calls increment when clicked.
Astro
Hint
Use <script setup> with defineProps to accept count and increment. Render the count and a button that calls increment on click.
4
Render both islands in Astro and share state
In your Astro page, import CounterReact and CounterVue. Render both components as islands, passing the shared count and increment function as props to each. Use client:load directive for hydration.
Astro
Hint
Import both components. Render them with client:load and pass count and increment as props.
Practice
(1/5)
1. What is the main purpose of sharing state between framework islands in Astro?
easy
A. To allow different interactive components to update and reflect changes together
B. To load all components on the server only
C. To prevent any user interaction on the page
D. To make the page static without any updates
Solution
Step 1: Understand what framework islands are
Framework islands are small interactive parts of a page that can update independently.
Step 2: Identify the role of shared state
Sharing state lets these islands communicate and update together smoothly.
Final Answer:
To allow different interactive components to update and reflect changes together -> Option A
Quick Check:
Sharing state = interactive sync [OK]
Hint: Sharing state means components update together [OK]
Common Mistakes:
Thinking sharing state disables interaction
Confusing server-only loading with state sharing
Assuming sharing state makes page static
2. Which of the following is the correct way to pass a shared signal as a prop to an island in Astro?
easy
A.
B.
C.
D.
Solution
Step 1: Recall correct JSX prop syntax
Props must be passed with curly braces for variables: propName={value}
Step 2: Identify correct client directive
client:load is a valid directive to hydrate the island on page load.
Final Answer:
<Island signal={sharedSignal} client:load /> -> Option A
Quick Check:
Props use curly braces + client:load [OK]
Hint: Use curly braces for props and client:load to hydrate [OK]
Common Mistakes:
Omitting curly braces around signal
Using wrong client directive syntax
Passing prop without curly braces
3. Given this Astro code snippet, what will be the output when clicking the button inside the island?
---
import { signal } from '@astrojs/signals';
const sharedCount = signal(0);
---
// Inside CounterIsland component:
// count.value++}>Clicked {count.value} times
medium
A. Clicking the button causes a runtime error
B. The button text never updates after the first render
C. The button text updates and shows the incremented count on each click
D. The count resets to zero on every click
Solution
Step 1: Understand signal behavior
Signals hold reactive state; updating count.value triggers UI update.
Step 2: Check client:load directive
client:load hydrates the island so events like onClick work.
Final Answer:
The button text updates and shows the incremented count on each click -> Option C
Quick Check:
Signal + client:load = reactive updates [OK]
Hint: Signals update UI reactively when hydrated with client:load [OK]
Common Mistakes:
Assuming signals don't update UI
Forgetting client:load disables interactivity
Thinking count resets on each click
4. You try to share a signal between two islands but the second island does not update when the signal changes. What is the most likely cause?
medium
A. The signal was declared inside the second island component
B. The signal value was mutated directly without using .value
C. The client directive is missing on the first island
D. The signal was not passed as a prop to the second island
Solution
Step 1: Check how signals are shared
Signals must be passed as props to islands to share state.
Step 2: Identify why second island doesn't update
If the second island doesn't receive the signal prop, it can't react to changes.
Final Answer:
The signal was not passed as a prop to the second island -> Option D
Quick Check:
Missing prop = no update [OK]
Hint: Always pass signals as props to all islands needing shared state [OK]
5. You want two islands to share a counter state that increments when either button is clicked. Which approach correctly shares and updates the state across both islands?
hard
A. Declare the signal inside one island and import it directly in the other island
B. Declare a shared signal in the parent Astro component and pass it as a prop to both islands with client:load
C. Use local state inside each island without passing props
D. Declare separate signals inside each island and sync them with events
Solution
Step 1: Understand shared state location
Shared signals must live in a common parent to be passed down.
Step 2: Pass the shared signal as props to both islands
Passing the same signal prop lets both islands update and reflect changes.
Step 3: Use client:load to hydrate both islands for interactivity
client:load enables event handling and reactive updates in islands.
Final Answer:
Declare a shared signal in the parent Astro component and pass it as a prop to both islands with client:load -> Option B
Quick Check:
Shared signal in parent + props + client:load = synced islands [OK]
Hint: Keep shared state in parent and pass to islands with client:load [OK]
Common Mistakes:
Declaring separate signals causing unsynced state
Not passing signal as prop to both islands
Importing signal directly between islands (not supported)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.