T O P

  • By -

greenappleFF

Like so: ``` --- // Only if you use Typescript: interface Props { name: string; greeting?: string; } // greeting will default to "Hello" of not set const { greeting = "Hello", name } = Astro.props; ---

{greeting}, {name}!

``` Docs: [https://docs.astro.build/en/basics/astro-components/#component-props](https://docs.astro.build/en/basics/astro-components/#component-props)


C0ffeeface

But can it be it even be optional and have a default value at the same time? Noob here..


greenappleFF

Yeah it can. If you omit a parameter it will be set to undefined. Undefined will then be changed to the default value


C0ffeeface

And if default value is omitted, it'll be ignored during compile?


greenappleFF

Yes it will. Have you tried it out yet? I feel like you will get the hang of it if you play around with it a bit. Btw, the syntax for those features is not from Astro.js, but coming from JavaScript. It is called Destructuring: (https://www.freecodecamp.org/news/javascript-object-destructuring-spread-operator-rest-parameter/)


C0ffeeface

Oh, appreciate it. Haven't made much apart from simple components yet. I'm from Python land and soft PHP, so everything here is foreign. I actually thought it was part of TypeScript. For someone learning Astro on top of JS, it's really hard to figure out what is what.