Astro Props

You might be familiar with the concept of props from a modern JavaScript framework like React, or Vue or Svelte.

NOTE: I wrote about all of those in the past, and you can find my articles on React Props, Vue Props and Svelte Props.

Props are the way we can pass information to components. This includes variables, but also functions.

Astro components also support props.

Here’s how to use them.

Suppose you define a Hello component in src/components/Hello.astro:

<p>Hello!</p>

You can pass a name prop to the component when you use it, like this: <Hello name="Flavio" />, and you can display the name in your component output by using this syntax:

<p>Hello {Astro.props.name}!</p>

It’s common to extract the props to individual variables with object destructuring in the component’s frontmatter section, which is nice when you have complex components:

---
const { name } = Astro.props
---
<p>Hello {name}!</p>

Here’s how to work with multiple props, to support for example this usage: <Hello name="Flavio" message="Welcome" />

---
const { name, message } = Astro.props
---
<p>{message} {name}!</p>

And in this way you can support defaults for props that might be unset:

---
const { name = '', message = 'Hello' } = Astro.props
---
<p>{message} {name}!</p>


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

You might be familiar with the concept of props from a modern JavaScript framework like React, or Vue or Svelte.

NOTE: I wrote about all of those in the past, and you can find my articles on React Props, Vue Props and Svelte Props.

Props are the way we can pass information to components. This includes variables, but also functions.

Astro components also support props.

Here’s how to use them.

Suppose you define a Hello component in src/components/Hello.astro:

<p>Hello!</p>

You can pass a name prop to the component when you use it, like this: <Hello name="Flavio" />, and you can display the name in your component output by using this syntax:

<p>Hello {Astro.props.name}!</p>

It’s common to extract the props to individual variables with object destructuring in the component’s frontmatter section, which is nice when you have complex components:

---
const { name } = Astro.props
---
<p>Hello {name}!</p>

Here’s how to work with multiple props, to support for example this usage: <Hello name="Flavio" message="Welcome" />

---
const { name, message } = Astro.props
---
<p>{message} {name}!</p>

And in this way you can support defaults for props that might be unset:

---
const { name = '', message = 'Hello' } = Astro.props
---
<p>{message} {name}!</p>


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-11-16T05:00:00+00:00) Astro Props. Retrieved from https://www.scien.cx/2021/11/16/astro-props/

MLA
" » Astro Props." flaviocopes.com | Sciencx - Tuesday November 16, 2021, https://www.scien.cx/2021/11/16/astro-props/
HARVARD
flaviocopes.com | Sciencx Tuesday November 16, 2021 » Astro Props., viewed ,<https://www.scien.cx/2021/11/16/astro-props/>
VANCOUVER
flaviocopes.com | Sciencx - » Astro Props. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/16/astro-props/
CHICAGO
" » Astro Props." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/11/16/astro-props/
IEEE
" » Astro Props." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/11/16/astro-props/. [Accessed: ]
rf:citation
» Astro Props | flaviocopes.com | Sciencx | https://www.scien.cx/2021/11/16/astro-props/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.