You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/docs/src/routes/docs/(qwik)/core/each/index.mdx
+11-25Lines changed: 11 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,9 @@ import CodeSandbox from '../../../../../components/code-sandbox/index.tsx';
9
9
10
10
`Each` is a built-in Qwik component for rendering keyed lists.
11
11
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).
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.
104
104
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.
<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>
124
124
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
+
<divkey={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:
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
+
125
174
### Rendering Conditionally
126
175
127
176
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.
0 commit comments