Bird
Raised Fist0
Astroframework~5 mins

Sharing state between framework islands in Astro

Choose your learning style10 modes available
Introduction

Sharing state between framework islands helps different parts of a webpage work together smoothly. It lets components built with different tools talk to each other.

You have a button in one island that changes data shown in another island.
You want to keep user info consistent across different parts of the page.
You need to update multiple components when a user interacts with one.
You want to avoid reloading the whole page to share data.
You want to build a fast, interactive site using multiple frameworks.
Syntax
Astro
import { createSignal } from 'solid-js';

// Create a shared state signal
const [state, setState] = createSignal(initialValue);

// Pass state and setState as props to islands
<IslandA state={state} setState={setState} />
<IslandB state={state} setState={setState} />

Use a shared state signal or store that multiple islands can access.

Pass the state and update functions as props to each island.

Examples
One island changes the count, another shows it.
Astro
// Create shared state
const [count, setCount] = createSignal(0);

// Island A increments count
<IslandA count={count} setCount={setCount} />

// Island B shows count
<IslandB count={count} />
Load the island on the client to use shared state interactively.
Astro
// Using Astro's client directives
<Island client:load>
  <Counter count={count} setCount={setCount} />
</Island>
Sample Program

This example shows two islands sharing the same count state. Clicking the button updates the count, and the display updates automatically.

Astro
import { createSignal } from 'solid-js';

const [count, setCount] = createSignal(0);

function IncrementButton({ count, setCount }) {
  return (
    <button onClick={() => setCount(count() + 1)} aria-label="Increment count">
      Increment
    </button>
  );
}

function DisplayCount({ count }) {
  return <p>Current count: {count()}</p>;
}

export default function App() {
  return (
    <main>
      <IncrementButton count={count} setCount={setCount} />
      <DisplayCount count={count} />
    </main>
  );
}
OutputSuccess
Important Notes

Make sure to use client directives like client:load to enable interactivity.

Passing state as props keeps islands in sync without full page reloads.

Use accessible labels like aria-label for better user experience.

Summary

Sharing state lets different islands work together smoothly.

Use shared signals and pass them as props to islands.

Client directives enable interactive updates on the page.

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

  1. Step 1: Understand what framework islands are

    Framework islands are small interactive parts of a page that can update independently.
  2. Step 2: Identify the role of shared state

    Sharing state lets these islands communicate and update together smoothly.
  3. Final Answer:

    To allow different interactive components to update and reflect changes together -> Option A
  4. 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

  1. Step 1: Recall correct JSX prop syntax

    Props must be passed with curly braces for variables: propName={value}
  2. Step 2: Identify correct client directive

    client:load is a valid directive to hydrate the island on page load.
  3. Final Answer:

    <Island signal={sharedSignal} client:load /> -> Option A
  4. 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

  1. Step 1: Understand signal behavior

    Signals hold reactive state; updating count.value triggers UI update.
  2. Step 2: Check client:load directive

    client:load hydrates the island so events like onClick work.
  3. Final Answer:

    The button text updates and shows the incremented count on each click -> Option C
  4. 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

  1. Step 1: Check how signals are shared

    Signals must be passed as props to islands to share state.
  2. 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.
  3. Final Answer:

    The signal was not passed as a prop to the second island -> Option D
  4. Quick Check:

    Missing prop = no update [OK]
Hint: Always pass signals as props to all islands needing shared state [OK]
Common Mistakes:
  • Declaring signal inside island instead of sharing
  • Forgetting client directive disables interactivity
  • Mutating signal incorrectly without .value
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

  1. Step 1: Understand shared state location

    Shared signals must live in a common parent to be passed down.
  2. Step 2: Pass the shared signal as props to both islands

    Passing the same signal prop lets both islands update and reflect changes.
  3. Step 3: Use client:load to hydrate both islands for interactivity

    client:load enables event handling and reactive updates in islands.
  4. 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
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard