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 start-here.md with gotchas on using x-for #4245

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/docs/src/en/start-here.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,40 @@ Also notice that `x-for` is declared on a `<template>` element and not directly

Now any element inside the `<template>` tag will be repeated for every item inside `filteredItems` and all expressions evaluated inside the loop will have direct access to the iteration variable (`item` in this case).

When using `x-for`, it is important that there is only one root element in your loop. Consider the following example:

```alpine
<div x-data="{ items: [1, 2, 3, 4, 5, 6] }">
<template x-for="index in items">
<template x-if="index % 2 === 0">
<p>Even, I will render</p>
</template>
<template x-if="index % 2 !== 0">
<p>Odd, I will *not* render!</p>
</template>
</template>
</div>
```

The first `<template>` in the loop will render fine. But you will get an error for the second one telling you that `index` is "undefined".

We can fix this by adding only a single root element inside our loop so that everything will work as expected.

```alpine
<div x-data="{ items: [1, 2, 3, 4, 5, 6] }">
<template x-for="index in items">
<div>
<template x-if="index % 2 === 0">
<p>Even, I will render</p>
</template>
<template x-if="index % 2 !== 0">
<p>Odd, I will render</p>
</template>
</div>
</template>
</div>
```

[→ Read more about `x-for`](/directives/for)

<a name="recap"></a>
Expand Down
Loading