Props let you send information to components so they can show different things. Component composition means building big parts by combining smaller parts.
Props and component composition in Astro
---
const { title, content } = Astro.props;
---
<h2>{title}</h2>
<p>{content}</p>Props are accessed inside the frontmatter script using Astro.props.
Component composition means using one component inside another by importing and including it in the template.
---
const { label } = Astro.props;
---
<button>{label}</button>---
const { user } = Astro.props;
---
<div>
<h3>{user.name}</h3>
<p>{user.email}</p>
</div>--- import Button from './Button.astro'; --- <section> <h1>Welcome</h1> <Button label="Click me" /> </section>
This example shows a Button component that takes a label prop. The Page component uses two Button components with different labels. This shows how props customize components and how composition builds UI.
---
// Button.astro
const { label } = Astro.props;
---
<button>{label}</button>
---
// Page.astro
import Button from './Button.astro';
---
<main>
<h1>My Page</h1>
<Button label="Save" />
<Button label="Cancel" />
</main>Always name props clearly so it is easy to understand what data the component expects.
Props are read-only inside the child component; do not try to change them there.
Use component composition to keep your code organized and reusable.
Props send data from parent to child components.
Component composition means building UI by combining smaller components.
This helps make your code cleaner and easier to manage.
Practice
props in Astro components?Solution
Step 1: Understand the role of props
Props are used to send data from a parent component to a child component in Astro.Step 2: Differentiate from other uses
Props are not for styling, event handling, or data fetching directly; those are separate concerns.Final Answer:
To pass data from a parent component to a child component -> Option AQuick Check:
Props = data passing [OK]
- Thinking props handle events
- Confusing props with CSS styling
- Assuming props fetch data
title with value "Hello" to a child component <MyComponent /> in Astro?Solution
Step 1: Recall Astro prop syntax
Props are passed as HTML-like attributes with string values in quotes.Step 2: Check each option
<MyComponent title="Hello" />uses correct syntax:title="Hello".<MyComponent {title: "Hello"} />uses object syntax which is invalid.<MyComponent title={Hello} />misses quotes around Hello.<MyComponent title=>Hello</MyComponent>uses children syntax which is not how props are passed.Final Answer:
<MyComponent title="Hello" /> -> Option DQuick Check:
Props = attribute="value" [OK]
- Using curly braces incorrectly
- Forgetting quotes around string values
- Trying to pass props as children
---
const { message } = Astro.props;
---
<div>{message}</div>What will be rendered if the parent uses
<Child message="Hi!" />?Solution
Step 1: Understand prop extraction
The component extractsmessagefromAstro.props, which receives the prop from the parent.Step 2: Check rendering output
The JSX-like syntax outputs the value ofmessageinside a <div>, so it renders <div>Hi!</div>.Final Answer:
<div>Hi!</div> -> Option BQuick Check:
Prop value = rendered content [OK]
- Thinking JSX shows literal {message}
- Assuming missing prop causes error
- Expecting empty div if prop exists
<ChildComponent message=Hello />
Solution
Step 1: Check prop syntax
Props with string values must be enclosed in quotes in Astro.Step 2: Identify the error
The code passesmessage=Hellowithout quotes, causing a syntax error.Final Answer:
Missing quotes around the string Hello -> Option CQuick Check:
String props need quotes [OK]
- Omitting quotes around strings
- Thinking props are passed as children
- Adding unnecessary semicolons
Item components, each receiving a name prop from an array ['Apple', 'Banana', 'Cherry']. Which Astro code correctly composes these components?Solution
Step 1: Understand how to loop in Astro
Astro supports JavaScript expressions inside braces, somapis used to create multiple components.Step 2: Check each option's correctness
--- const fruits = ['Apple', 'Banana', 'Cherry']; --- {fruits.map(fruit => )}correctly maps overfruitsand passes eachfruitas anameprop.--- const fruits = ['Apple', 'Banana', 'Cherry']; --- <Item name="fruits" />
passes the whole array as a string prop, not individual items.--- const fruits = ['Apple', 'Banana', 'Cherry']; --- {fruits.forEach(fruit => )}usesforEachwhich returns undefined, so no output.--- const fruits = ['Apple', 'Banana', 'Cherry']; --- <Item>{fruits}</Item>passes the whole array as children, not as props.Final Answer:
Use map to create multiple Item components with name props -> Option AQuick Check:
Use map for lists in JSX [OK]
- Using forEach instead of map
- Passing whole array as one prop
- Passing array as children instead of props
Ready to prove it?
Pass the test and this pattern moves unknown
