Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update "Fragments & Multiple Elements" examples #778

Merged
merged 9 commits into from
Jun 22, 2022
20 changes: 18 additions & 2 deletions src/pages/en/core-concepts/astro-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,17 @@ const items = ["Dog", "Cat", "Platypus"];

#### Fragments & Multiple Elements

Remember: an Astro component template can render multiple elements with no need to wrap everything in a single `<div>` or `<>`.
An Astro component template can render multiple elements with no need to wrap everything in a single `<div>` or `<>`, unlike JavaScript or JSX.
sarah11918 marked this conversation as resolved.
Show resolved Hide resolved

However, when using an Astro JSX-like expression to dynamically create elements, you must wrap these multiple elements inside of a **Fragment** just like you would in JavaScript or JSX. Astro supports using either `<Fragment> </Fragment>` or `<> </>`.
```astro
---
// Template with multiple elements
---
<p>No need to wrap elements in a single containing element.</p>
<p>Astro supports multiple root elements in a template.</p>
```

However, when using an expression to dynamically create multiple elements, you should wrap these elements inside a **fragment** as you would in JavaScript or JSX. Astro supports using either `<Fragment> </Fragment>` or the shorthand `<> </>`.

```astro
---
Expand All @@ -173,6 +181,14 @@ const items = ["Dog", "Cat", "Platypus"];
</ul>
```

Fragments can also be useful to avoid wrapper elements when adding [`set:*` directives](/en/reference/directives-reference/#sethtml), as in the following example:

```astro
---
const htmlString = '<p>Raw HTML content</p>';
---
<Fragment set:html={htmlString} />
sarah11918 marked this conversation as resolved.
Show resolved Hide resolved
```

### Component Props

Expand Down