📖 Scenario: You are building a simple blog page using Astro. You have a collection of blog posts stored as data, and you want to display the title of each post on the page.
🎯 Goal: Create an Astro component that renders a list of blog posts from a collection. Each post should show its title.
📋 What You'll Learn
Create a collection of blog posts as an array of objects with exact keys and values.
Add a configuration variable to filter posts by year.
Use posts.map() to render only posts from the specified year.
Complete the Astro component with proper HTML structure to display the filtered posts.
💡 Why This Matters
🌍 Real World
Rendering collections of data like blog posts or products is common in websites and apps to show dynamic content.
💼 Career
Knowing how to filter and render collections in frameworks like Astro is useful for frontend developer roles.
Progress0 / 4 steps
1
Create the blog posts collection
Create a constant called posts that is an array of objects. Each object must have the exact keys and values: { title: 'Post One', date: '2023-01-15' }, { title: 'Post Two', date: '2023-05-20' }, and { title: 'Post Three', date: '2022-12-10' }.
Astro
Hint
Use const posts = [ ... ] with three objects inside the array.
2
Add a filter year variable
Add a constant called filterYear and set it to the string '2023'.
Astro
Hint
Use const filterYear = '2023'; to set the year filter.
3
Filter posts by year and render titles
Use posts.map(post => ...) to iterate over posts. Use the ternary operator to check if post.date starts with filterYear and render a <li> element with the post's title inside a <ul> for each matching post.
Astro
Hint
Use posts.map(post => ...) and check post.date.startsWith(filterYear) to filter posts.
4
Complete the Astro component
Wrap the code in an Astro component by adding the frontmatter tags --- around the JavaScript code. Ensure the <ul> list with filtered posts is outside the frontmatter.
Astro
Hint
Make sure to add --- before and after the JavaScript code to define the Astro frontmatter.
Practice
(1/5)
1. What does Astro.fetchContent do in rendering collection content?
easy
A. It deletes items from the database.
B. It styles the collection items automatically.
C. It fetches data from a collection to display on the page.
D. It creates a new collection in the database.
Solution
Step 1: Understand the purpose of Astro.fetchContent
This function is used to get data from a collection, so you can use it in your page.
Step 2: Identify what happens with the data
The fetched data is then used to show each item on the page, not to style, delete, or create collections.
Final Answer:
It fetches data from a collection to display on the page. -> Option C
Quick Check:
Astro.fetchContent fetches data [OK]
Hint: Remember: fetch means get data, not change it [OK]
Common Mistakes:
Thinking fetchContent styles or deletes data
Confusing fetching with creating collections
2. Which of the following is the correct syntax to loop through a collection named posts in Astro to render each item?
easy
A. {posts.each(post => <div>{post.title}</div>)}
B. {posts.forEach(post => <div>{post.title}</div>)}
C. {posts.loop(post => <div>{post.title}</div>)}
D. {posts.map(post => <div>{post.title}</div>)}
Solution
Step 1: Identify the correct method to transform arrays in Astro
Astro uses JavaScript array methods; .map() returns a new array of elements for rendering.
Step 2: Check other options for correctness
forEach does not return an array, so it won't render correctly. loop and each are not valid JavaScript array methods.
Final Answer:
{posts.map(post => <div>{post.title}</div>)} -> Option D
Quick Check:
Use .map() to render collections [OK]
Hint: Use .map() to create elements from arrays [OK]
Common Mistakes:
Using forEach which returns undefined
Using non-existent methods like loop or each
3. Given the collection products with items [{name: 'Pen'}, {name: 'Book'}], what will this Astro code output?
{products.map(p => `
${p.name}
`).join('')}
medium
A.
Pen
Book
B. [object Object][object Object]
C.
products
D. SyntaxError
Solution
Step 1: Understand the map function output
Each product's name is wrapped in <p> tags, creating an array of strings: ['
Pen
', '
Book
'].
Step 2: Understand the join('') method
Joining the array with empty string combines the strings into one: '
Pen
Book
'.
Final Answer:
<p>Pen</p><p>Book</p> -> Option A
Quick Check:
map + join creates combined HTML string [OK]
Hint: map returns array; join('') combines to string [OK]
Common Mistakes:
Expecting array output instead of joined string
Confusing object output with string output
4. You wrote this code to render a collection but get an error:
B. Astro.fetchContent returns a Promise, but you didn't await it.
C. You forgot to import Astro.
D. You cannot use return inside Astro components.
Solution
Step 1: Check how Astro.fetchContent works
This function returns a Promise, so you must await it to get the data array.
Step 2: Identify the error cause
Without await, items is a Promise object, so calling map on it causes an error.
Final Answer:
Astro.fetchContent returns a Promise, but you didn't await it. -> Option B
Quick Check:
Always await async fetchContent calls [OK]
Hint: Always await fetchContent to get data array [OK]
Common Mistakes:
Not awaiting async fetchContent
Confusing map and forEach usage
Assuming fetchContent returns data directly
5. You want to render a list of blog posts but only show posts with a published flag set to true. Which Astro code correctly fetches and renders only published posts from the blogs collection?