Bird
Raised Fist0
Astroframework~10 mins

Sharing state between framework islands in Astro - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the state management function in Astro.

Astro
import { [1] } from 'astro/client';
Drag options to blanks, or click blank then click option'
AuseEffect
BuseState
CuseSignal
DuseContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using React hooks like useState which are not available in Astro islands.
Confusing useEffect with state creation.
2fill in blank
medium

Complete the code to create a shared signal named 'count' with initial value 0.

Astro
const count = [1](0);
Drag options to blanks, or click blank then click option'
AuseSignal
BuseState
CcreateSignal
DuseEffect
Attempts:
3 left
💡 Hint
Common Mistakes
Using React's createSignal which is not available in Astro.
Using useState which is React-specific.
3fill in blank
hard

Fix the error in passing the shared signal as a prop to a child island.

Astro
<ChildIsland count=[1] />
Drag options to blanks, or click blank then click option'
Acount
Bcount.value
Ccount()
Dcount.current
Attempts:
3 left
💡 Hint
Common Mistakes
Passing count.value which is undefined in Astro signals.
Calling count() which is not a function in Astro signals.
4fill in blank
hard

Fill both blanks to update the shared signal value inside a child island event handler.

Astro
function increment() {
  [1].value = [2].value + 1;
}
Drag options to blanks, or click blank then click option'
Aprops.count
Bcount
Cprops.count.value
Dcount.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using a local variable 'count' instead of the prop.
Trying to call the signal as a function.
5fill in blank
hard

Fill all three blanks to create a shared signal, pass it to a child island, and update it on a button click.

Astro
const [1] = [2](0);

return <ChildIsland count=[3] />;
Drag options to blanks, or click blank then click option'
Acount
BuseSignal
DuseState
Attempts:
3 left
💡 Hint
Common Mistakes
Using useState instead of useSignal.
Passing the signal value instead of the signal itself.

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