📖 Scenario: You are building a simple product showcase page using Astro. Some parts of the page need to be interactive, like a button that shows an alert when clicked. Other parts are static and do not need any JavaScript on the client side.
🎯 Goal: Learn how to decide when to hydrate components for interactivity and when to keep components static in Astro. You will create a static product list and an interactive button that only hydrates when needed.
📋 What You'll Learn
Create a static product list component in Astro
Create an interactive button component that shows an alert on click
Use Astro's hydration directives to hydrate the button only on client
Keep the product list static with no client-side JavaScript
💡 Why This Matters
🌍 Real World
Websites often have parts that are static and parts that need interactivity. Knowing when to hydrate components helps build fast, efficient sites.
💼 Career
Front-end developers use hydration strategies to optimize performance and user experience in modern frameworks like Astro.
Progress0 / 4 steps
1
Create a static product list component
Create an Astro component called ProductList.astro with a products array containing these exact objects: { id: 1, name: 'Coffee Mug' }, { id: 2, name: 'Notebook' }, and { id: 3, name: 'Pen' }. Render an unordered list <ul> with each product name inside a <li>.
Astro
Hint
Use an array called products with the exact objects. Use {products.map(product => ( ... ))} to render the list.
2
Create an interactive button component
Create an Astro component called AlertButton.astro that renders a button with the text Click me. Add a client-side click event handler that shows an alert with the message 'Button clicked!'.
Astro
Hint
Define a function handleClick that calls alert('Button clicked!'). Attach it to the button's onClick event.
3
Hydrate the button only on client
In your main page Index.astro, import ProductList and AlertButton. Render <ProductList /> as static with no hydration. Render <AlertButton /> with the hydration directive client:load to hydrate it only on the client side.
Astro
Hint
Use client:load on <AlertButton /> to hydrate it only on the client. Render <ProductList /> normally.
4
Keep product list static with no client JavaScript
Verify that ProductList is rendered without any hydration directive so it stays static and does not load JavaScript on the client. Confirm that only AlertButton uses client:load hydration.
Astro
Hint
Do not add any hydration directive to <ProductList />. Only <AlertButton /> should have client:load.
Practice
(1/5)
1. Which situation is best for keeping a component static in Astro?
easy
A. When the component only displays information and has no user interaction
B. When the component needs to update frequently based on user input
C. When the component requires real-time data fetching
D. When the component has buttons that trigger events
Solution
Step 1: Understand static components
Static components do not need to respond to user actions or update dynamically.
Step 2: Identify when to keep static
If a component only shows fixed information without interactivity, it should remain static for better performance.
Final Answer:
When the component only displays information and has no user interaction -> Option A
Quick Check:
Static = no interactivity [OK]
Hint: Static means no user interaction needed [OK]
Common Mistakes:
Thinking all components must hydrate
Hydrating components without user actions
Confusing static with dynamic updates
2. Which Astro syntax correctly hydrates a component only on client load?
easy
A. <MyComponent />
B. <MyComponent client:idle />
C. <MyComponent client:visible />
D. <MyComponent client:load />
Solution
Step 1: Recall hydration directives
Astro uses client:load to hydrate a component immediately after the page loads.
Step 2: Match syntax to behavior
client:load hydrates on load, client:idle waits for idle, client:visible hydrates when visible, and no directive means no hydration.
Final Answer:
<MyComponent client:load /> -> Option D
Quick Check:
client:load = hydrate on load [OK]
Hint: client:load hydrates immediately after page load [OK]
Common Mistakes:
Using client:idle when immediate hydration is needed
Omitting hydration directive expecting auto hydration
Confusing client:visible with client:load
3. Given this Astro component usage:
<Counter client:idle />
What happens when the page loads?
medium
A. The Counter component hydrates immediately and is interactive
B. The Counter component never hydrates and stays static
C. The Counter component hydrates only when the browser is idle
D. The Counter component hydrates only when visible on screen
Solution
Step 1: Understand client:idle behavior
The client:idle directive delays hydration until the browser is idle, improving performance.
Step 2: Predict component behavior on load
On page load, the component is static until the browser has free time, then it hydrates and becomes interactive.
Final Answer:
The Counter component hydrates only when the browser is idle -> Option C
Quick Check:
client:idle = hydrate on idle [OK]
Hint: client:idle waits for browser idle before hydrating [OK]
Common Mistakes:
Assuming immediate hydration with client:idle
Thinking client:idle means no hydration
Confusing client:idle with client:visible
4. You wrote this Astro code:
<Button client:load>Click</Button>
But the button is not interactive. What is the likely problem?
medium
A. You forgot to import the Button component
B. You used client:load on a static HTML element
C. You did not export the Button component properly
D. You need to add client:visible instead of client:load
Solution
Step 1: Check hydration on static elements
Hydration directives like client:load only work on Astro components, not plain HTML elements.
Step 2: Identify the problem in code
Using client:load on a native HTML tag like <Button> (instead of a component) does nothing, so no interactivity.
Final Answer:
You used client:load on a static HTML element -> Option B
Quick Check:
Hydrate only Astro components, not plain HTML [OK]
Hint: Hydrate only Astro components, not HTML tags [OK]
Common Mistakes:
Assuming hydration works on native HTML tags
Not importing components correctly
Confusing client:load with client:visible
5. You have a dashboard with a live chat and a static news feed. How should you hydrate these in Astro for best performance?
hard
A. Keep news feed static and hydrate chat with client:visible
B. Hydrate news feed with client:idle and chat static
C. Hydrate both chat and news feed immediately with client:load
D. Keep both static and add hydration later if needed
Solution
Step 1: Analyze component interactivity needs
The live chat requires interactivity, so it needs hydration. The news feed is static content and does not need hydration.
Step 2: Choose hydration strategy for performance
Hydrating chat with client:visible delays hydration until visible, improving load speed. Keeping news feed static avoids unnecessary hydration.
Final Answer:
Keep news feed static and hydrate chat with client:visible -> Option A