Skip to content

Commit 8ab6101

Browse files
committed
docs: update Each documentation and menu reference for clarity and optimization guidance
1 parent b29618e commit 8ab6101

3 files changed

Lines changed: 63 additions & 28 deletions

File tree

packages/docs/src/routes/docs/(qwik)/core/each/index.mdx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import CodeSandbox from '../../../../../components/code-sandbox/index.tsx';
99

1010
`Each` is a built-in Qwik component for rendering keyed lists.
1111

12-
It is most useful when list items have a stable identity and you want Qwik to preserve and move existing rows instead of re-rendering the whole list when the order changes.
12+
For most list rendering, write normal `items.map()` code and let Qwik optimize compatible keyed loops automatically.
13+
14+
This page documents the manual `Each` API for the cases where you want to use it explicitly. For the default list-rendering guidance, see [Rendering](../rendering/index.mdx).
1315

1416
<CodeSandbox src="/src/routes/demo/component/each/index.tsx" style={{ height: '10em' }}>
1517
```tsx /Each/ /key$/ /item$/
@@ -76,33 +78,17 @@ export default component$(() => {
7678
});
7779
```
7880

79-
## `Each` vs `items.map()`
80-
81-
If your list items have stable keys, prefer `Each`.
82-
83-
It is the specialized keyed-list primitive in Qwik and is designed to preserve and move existing rows efficiently.
84-
85-
Use `items.map()` for simple list rendering or when you do not have a stable key:
86-
87-
```tsx
88-
<ul>
89-
{todos.value.map((todo) => (
90-
<li key={todo.id}>{todo.label}</li>
91-
))}
92-
</ul>
93-
```
81+
## When to use manual `Each`
9482

95-
Prefer `Each` when:
83+
Most applications should use `items.map()` in component templates.
9684

97-
- items have stable ids
98-
- rows are frequently reordered, inserted, or removed
99-
- you want to preserve existing row DOM and component instances
85+
Manual `Each` is useful when:
10086

101-
Prefer `map()` when:
87+
- you want to make the keyed-list behavior explicit in the source
88+
- you are building a reusable abstraction around `Each`
89+
- you prefer the explicit `items`, `key$`, and `item$` API shape for a specific list
10290

103-
- there is no stable key for the item
104-
- you want ordinary JSX list rendering without `Each`'s keyed-row preservation behavior
105-
- the row output should be recomputed from replaced item objects even when the key stays the same
91+
If you want the usual authoring experience, use `items.map()` and let the optimizer rewrite compatible loops for you.
10692

10793
## How keyed updates work
10894

@@ -127,5 +113,5 @@ Using unstable keys defeats the main benefit of `Each` and can produce confusing
127113

128114
## Related
129115

130-
- For general list rendering, see [Rendering](../rendering/index.mdx)
116+
- For default list rendering with `items.map()`, automatic optimization, warnings, and opt-out comments, see [Rendering](../rendering/index.mdx)
131117
- For the generated API entry, see [API Reference](/api/qwik/#each)

packages/docs/src/routes/docs/(qwik)/core/rendering/index.mdx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ export const Child = component$((props: { name: string }) => {
100100

101101
### Rendering a list of items
102102

103-
Qwik supports rendering lists with either `items.map()` or the [`Each`](../each/index.mdx) component.
103+
Write lists with normal `items.map()` in component templates.
104104

105-
For keyed collections, prefer [`Each`](../each/index.mdx). If you render with `items.map()`, every item in the list must have a unique `key` property on the first child returned by the mapping function. The `key` must be a string or number and must be unique within the list.
105+
If you render with `items.map()`, every item in the list must have a unique `key` property on the first child returned by the mapping function. The `key` must be a string or number and must be unique within the list.
106106

107107
```tsx {6} /data.map/ /key/#a
108108
import { component$ } from '@qwik.dev/core';
@@ -122,6 +122,55 @@ export const Parent = component$(() => {
122122

123123
<Note>It is not recommended to use the array's index as the key unless you can guarantee that the data for a given key will always be the same. It is always preferred to use some unique identifier from the data as the key.</Note>
124124

125+
## Automatic `.map()` optimization
126+
127+
For compatible render loops, Qwik optimizes keyed `items.map()` calls into an internal [`Each`](../each/index.mdx)-style keyed list automatically during compilation.
128+
129+
The optimization applies when:
130+
131+
- the callback returns a single JSX node
132+
- the first returned node has a `key`
133+
- the key does not use the callback's index parameter
134+
- the key is not derived from a function call
135+
136+
For example, this authoring pattern:
137+
138+
```tsx
139+
{items.value.map((item) => (
140+
<div key={item.id}>{item.label}</div>
141+
))}
142+
```
143+
144+
can be optimized to the same keyed-list machinery used by manual `Each`.
145+
146+
If a `.map()` looks like a keyed list render but cannot be optimized, Qwik emits a `map-to-each` optimizer warning with the reason. If the conditions are not met, the original `.map()` stays unchanged.
147+
148+
Common reasons for the warning are:
149+
150+
- the returned JSX node is missing a `key`
151+
- the key uses the callback's index parameter
152+
- the key comes from a function call
153+
- the callback does not return a single JSX node
154+
155+
## Disabling the optimization
156+
157+
If you want to opt out for a specific render loop, use:
158+
159+
```tsx
160+
{
161+
/* @qwik-disable-next-line map-to-each */
162+
items.map((item) => <div key={item.id}>{item.label}</div>)
163+
}
164+
```
165+
166+
That disables the optimization and silences `map-to-each` warnings for that call.
167+
168+
## Manual `Each`
169+
170+
Manual [`Each`](../each/index.mdx) is still available as a public API, but it is now the explicit or advanced form rather than the default recommendation.
171+
172+
Use manual `Each` when you want to make the keyed-list behavior explicit in the source or when you are building an abstraction around its `items`, `key$`, and `item$` props.
173+
125174
### Rendering Conditionally
126175

127176
Conditional rendering is done with the Javascipt [ternary operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) `?`, the `&&` operator, or just by using `if` statements.

packages/docs/src/routes/docs/menu.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
- [Tasks & Lifecycle](</docs/(qwik)/core/tasks/index.mdx>)
1717
- [Context](</docs/(qwik)/core/context/index.mdx>)
1818
- [Slots](</docs/(qwik)/core/slots/index.mdx>)
19-
- [Each](</docs/(qwik)/core/each/index.mdx>)
2019
- [Rendering](</docs/(qwik)/core/rendering/index.mdx>)
20+
- [Each (Reference)](</docs/(qwik)/core/each/index.mdx>)
2121
- [Styling](</docs/(qwik)/core/styles/index.mdx>)
2222
- [API Reference](/api/qwik/)
2323

0 commit comments

Comments
 (0)