0
0
Astroframework~20 mins

client:media for responsive interactivity in Astro - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Astro Responsive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does client:media affect component rendering?

Consider an Astro component using client:media with a media query (min-width: 600px). What happens when the viewport width is 500px?

Astro
<script>
  export let message = "Hello!";
</script>

<div client:media="(min-width: 600px)">{message}</div>
AThe component does not hydrate or run on the client because the media query is false.
BThe component hydrates and runs on the client regardless of viewport size.
CThe component throws an error because the media query is invalid at 500px.
DThe component renders twice, once server-side and once client-side.
Attempts:
2 left
💡 Hint

Think about when client:media activates the component based on the media query.

state_output
intermediate
2:00remaining
What is the output when using client:media with dynamic state?

Given this Astro component that toggles a message on button click, but uses client:media="(min-width: 800px)", what will happen if the viewport is 700px wide and the button is clicked?

Astro
<script type="jsx" client:media="(min-width: 800px)">
  import { useState } from 'react';
  export default function Toggle() {
    const [show, setShow] = useState(false);
    return (
      <>
        <button onClick={() => setShow(!show)}>Toggle</button>
        {show && <p>Visible</p>}
      </>
    );
  }
</script>
AThe message toggles normally because the component always hydrates regardless of media query.
BNothing happens because the component is not hydrated at 700px, so clicks do not trigger state changes.
CThe component throws a runtime error because state is used with client:media.
DThe message is visible by default even before clicking the button.
Attempts:
2 left
💡 Hint

Remember how client:media controls hydration based on viewport size.

📝 Syntax
advanced
2:00remaining
Identify the correct syntax for client:media usage in Astro

Which of the following is the correct way to use client:media to hydrate a component only on screens smaller than 500px?

A<MyComponent client:media='max-width: 500px' />
B<MyComponent client:media={(max-width: 500px)} />
C<MyComponent client:media="(max-width: 500px)" />
D<MyComponent client:media="max-width: 500px" />
Attempts:
2 left
💡 Hint

Check the quotes and parentheses usage for attribute values in Astro components.

🔧 Debug
advanced
2:00remaining
Why does this client:media component fail to hydrate?

Given this Astro component, why does it fail to hydrate when the viewport is outside the 400-600px range?

Astro
<script type="jsx" client:media="(min-width: 400px) and (max-width: 600px)">
  export default function Info() {
    return <p>Info visible only between 400px and 600px</p>;
  }
</script>
AThe media query syntax is invalid because <code>and</code> must be uppercase <code>AND</code>.
BThe component fails because <code>client:media</code> requires a JavaScript import to work.
CThe component fails because <code>client:media</code> only supports single media features, not combined queries.
DThe component does not hydrate because the media query is never true if viewport is outside 400-600px range.
Attempts:
2 left
💡 Hint

Consider when the media query matches the viewport size.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using client:media in Astro?

Choose the best explanation for why client:media is useful in building responsive web apps with Astro.

A<code>client:media</code> delays hydration until the media query matches, improving performance by avoiding unnecessary JavaScript on unsupported screen sizes.
B<code>client:media</code> replaces CSS media queries by controlling layout purely with JavaScript.
C<code>client:media</code> forces all components to hydrate immediately regardless of screen size for consistency.
D<code>client:media</code> automatically resizes images based on screen size without additional code.
Attempts:
2 left
💡 Hint

Think about hydration and performance in responsive design.