Bird
Raised Fist0
Astroframework~20 mins

Layout components in Astro - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
šŸŽ–ļø
Astro Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ component_behavior
intermediate
2:00remaining
What is the rendered output of this Astro layout component?

Given the following Astro layout component and page, what will be the visible content in the browser?

Astro
---
// src/layouts/BaseLayout.astro
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <title>{title}</title>
  </head>
  <body>
    <header><h1>{title}</h1></header>
    <main>
      <slot />
    </main>
    <footer>Ā© 2024</footer>
  </body>
</html>

---
// src/pages/index.astro
---
import BaseLayout from '../layouts/BaseLayout.astro';
---
<BaseLayout title="Welcome">
  <p>Hello, Astro!</p>
</BaseLayout>
AA blank page with no visible content.
BA page with only the paragraph 'Hello, Astro!' and no header or footer.
CA page with a header 'Welcome' and footer 'Ā© 2024' but no paragraph content.
DA page with a header 'Welcome', a paragraph 'Hello, Astro!', and a footer 'Ā© 2024'.
Attempts:
2 left
šŸ’” Hint

Remember that <slot /> inserts the children content inside the layout.

šŸ“ Syntax
intermediate
1:30remaining
Which option correctly passes props to an Astro layout component?

Choose the correct way to pass a title prop to a layout component in Astro.

Astro
---
// src/layouts/PageLayout.astro
const { title } = Astro.props;
---
<h1>{title}</h1>
<slot />
A
&lt;PageLayout title="My Page"&gt;
  &lt;p&gt;Content&lt;/p&gt;
&lt;/PageLayout&gt;
B
&lt;PageLayout title={My Page}&gt;
  &lt;p&gt;Content&lt;/p&gt;
&lt;/PageLayout&gt;
C
&lt;PageLayout title='My Page'&gt;
  &lt;p&gt;Content&lt;/p&gt;
&lt;/PageLayout&gt;
D
&lt;PageLayout title=&gt;
  &lt;p&gt;Content&lt;/p&gt;
&lt;/PageLayout&gt;
Attempts:
2 left
šŸ’” Hint

Props in Astro components use double quotes for string values.

ā“ state_output
advanced
2:30remaining
What is the output when using reactive state inside an Astro layout?

Consider this Astro layout using a reactive counter. What will be displayed when the page loads?

Astro
---
// src/layouts/CounterLayout.astro
import { useState } from 'astro/jsx-runtime';
const [count, setCount] = useState(0);
---
<div>
  <p>Count: {count}</p>
  <button onClick={() => setCount(count + 1)}>Increment</button>
  <slot />
</div>
AThe page shows 'Count: 0' and a button labeled 'Increment'. Clicking the button updates the count dynamically.
BThe page shows 'Count: undefined' and a button labeled 'Increment'.
CThe page shows 'Count: 0' and a button labeled 'Increment', but clicking the button does nothing.
DThe page throws a runtime error because useState is not supported in Astro layouts.
Attempts:
2 left
šŸ’” Hint

Astro layouts are server-rendered and do not support client-side state updates by default.

šŸ”§ Debug
advanced
2:00remaining
Why does this Astro layout fail to render the child content?

Look at this Astro layout and page. The child content does not appear on the page. What is the cause?

Astro
---
// src/layouts/MissingSlot.astro
---
<html>
  <body>
    <h1>Title</h1>
    <!-- Missing slot here -->
  </body>
</html>

---
// src/pages/about.astro
---
import MissingSlot from '../layouts/MissingSlot.astro';
---
<MissingSlot>
  <p>About us content</p>
</MissingSlot>
AThe layout does not include a <slot /> element, so child content is not rendered.
BThe page forgot to import the layout component correctly.
CThe child content is outside the <body> tag, so it is ignored.
DAstro layouts cannot render child content inside <html> tags.
Attempts:
2 left
šŸ’” Hint

Check if the layout uses <slot /> to render children.

🧠 Conceptual
expert
3:00remaining
How does Astro handle nested layouts with slots?

Given nested layouts where LayoutA wraps LayoutB, which wraps the page content, how does Astro render the final output?

Assume each layout uses a <slot /> to render children.

Astro
---
// src/layouts/LayoutA.astro
---
<div class="a">
  <h1>Layout A</h1>
  <slot />
</div>

---
// src/layouts/LayoutB.astro
---
<div class="b">
  <h2>Layout B</h2>
  <slot />
</div>

---
// src/pages/home.astro
---
import LayoutA from '../layouts/LayoutA.astro';
import LayoutB from '../layouts/LayoutB.astro';
---
<LayoutA>
  <LayoutB>
    <p>Page content</p>
  </LayoutB>
</LayoutA>
AThe output shows only LayoutA's content and ignores LayoutB and page content.
BThe output nests LayoutA's div, then LayoutB's div inside it, then the page content inside LayoutB.
CThe output shows LayoutB's content wrapping LayoutA's content and page content inside.
DThe output duplicates the page content twice inside both layouts.
Attempts:
2 left
šŸ’” Hint

Think about how slots pass children down through nested layouts.

Practice

(1/5)
1. What is the main purpose of a layout component in Astro?
easy
A. To fetch data from an API
B. To define routes for the website
C. To handle user input events
D. To wrap page content and keep design consistent across pages

Solution

  1. Step 1: Understand what layout components do

    Layout components are used to wrap page content to maintain a consistent look and feel across multiple pages.
  2. Step 2: Compare with other options

    Fetching data, handling user input, and defining routes are not the main roles of layout components.
  3. Final Answer:

    To wrap page content and keep design consistent across pages -> Option D
  4. Quick Check:

    Layout = consistent design wrapper [OK]
Hint: Layouts wrap content to keep pages looking the same [OK]
Common Mistakes:
  • Confusing layout with data fetching
  • Thinking layouts handle user events
  • Mixing layout with routing logic
2. Which of the following is the correct way to use a layout component in an Astro page?
easy
A. import Layout from '../layouts/Layout.astro'; then wrap content with <Layout>...</Layout>
B. import Layout from '../layouts/Layout.astro'; then call Layout() as a function
C. Use <layout>...</layout> without import
D. Add layout='Layout' attribute to the <body> tag

Solution

  1. Step 1: Identify correct import and usage

    In Astro, you import the layout component and wrap your page content inside its tags.
  2. Step 2: Check other options for syntax errors

    Calling layout as a function or using tags without import is incorrect. Adding attributes to <body> is not how layouts work.
  3. Final Answer:

    Import layout and wrap content with <Layout>...</Layout> -> Option A
  4. Quick Check:

    Import + wrap content = correct layout use [OK]
Hint: Always import layout and wrap content inside its tags [OK]
Common Mistakes:
  • Trying to call layout as a function
  • Using layout tags without importing
  • Misusing HTML attributes for layouts
3. Given this layout component code:
---
// Layout.astro
---

  
    
Site Header
<slot />
Site Footer

And this page code:
---
import Layout from '../layouts/Layout.astro';
---
<Layout>
  <main>Welcome to my site</main>
</Layout>

What will the rendered HTML output look like?
medium
A.
Site Header
Welcome to my site
Site Footer
B.
Site Header
Site Footer
Welcome to my site
C.
Welcome to my site
D.
Site Header
Site Footer

Solution

  1. Step 1: Understand how <slot /> works in layout

    The <slot /> tag is replaced by the page content wrapped inside the layout.
  2. Step 2: Replace <slot /> with page content

    The page content is <main>Welcome to my site</main>, so it appears between header and footer.
  3. Final Answer:

    <html><body><header>Site Header</header><main>Welcome to my site</main><footer>Site Footer</footer></body></html> -> Option A
  4. Quick Check:

    Slot replaced by page content =
    Site Header
    Welcome to my site
    Site Footer
    [OK]
Hint: Slot tag shows page content inside layout [OK]
Common Mistakes:
  • Leaving tag as is in output
  • Placing page content outside footer
  • Omitting header or footer
4. This Astro page code tries to use a layout but shows an error:
---
import Layout from '../layouts/Layout'
---
<Layout>
  <h1>Hello</h1>
</Layout>

What is the most likely cause of the error?
medium
A. Using <Layout> without importing it
B. Missing semicolon after import statement
C. Layout component file path is incorrect or missing file extension
D. Page content must be inside a <main> tag

Solution

  1. Step 1: Check import statement syntax

    The import is missing the file extension '.astro', which is required in Astro imports.
  2. Step 2: Understand Astro import rules

    Astro requires full file paths including extensions for components, so '../layouts/Layout.astro' is correct, but '../layouts/Layout' is not.
  3. Final Answer:

    Layout component file path is incorrect or missing file extension -> Option C
  4. Quick Check:

    Missing file extension causes import error [OK]
Hint: Always include .astro extension in import paths [OK]
Common Mistakes:
  • Assuming semicolon is required in Astro imports
  • Forgetting to import layout before use
  • Thinking page content must be inside <main>
5. You want to create a layout that adds a sidebar only on some pages. Which approach correctly applies this conditional layout in Astro?
hard
A. Add sidebar HTML directly in each page without using layouts
B. Create two layouts: one with sidebar and one without; import and use the right one per page
C. Wrap the sidebar in a tag and pass it from the page
D. Use a single layout and add an if statement inside the layout to show sidebar based on page content

Solution

  1. Step 1: Understand layout usage for conditional design

    Layouts wrap pages, so to conditionally add sidebar, create separate layouts for each design.
  2. Step 2: Evaluate other options

    Adding if inside layout is complex and not recommended; passing sidebar via slot is not typical; adding sidebar in pages breaks consistency.
  3. Final Answer:

    Create two layouts: one with sidebar and one without; import and use the right one per page -> Option B
  4. Quick Check:

    Use different layouts for different page designs [OK]
Hint: Use separate layouts for different page designs [OK]
Common Mistakes:
  • Trying to conditionally render sidebar inside one layout
  • Misusing slots to pass sidebar content
  • Adding sidebar directly in pages breaking consistency
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