For
The For component iterates over the provided array and invokes the child
callback for each item. It can optionally be provided with a joiner which
is placed between each item, and an ender which is placed after the last
item when there is at least one item.
import { For } from "@alloy-js/core";
<For comma doubleHardline each={T} ender={Children} enderPunctuation hardline joiner={Children} line literalline semicolon skipFalsy softline space> {children}</For>import { For } from "@alloy-js/core/stc";
For({ comma: boolean, doubleHardline: boolean, each: T, ender: Children, enderPunctuation: boolean, hardline: boolean, joiner: Children, line: boolean, literalline: boolean, semicolon: boolean, skipFalsy: boolean, softline: boolean, space: boolean,}).children(children)| children | (…args: […ForCallbackArgs<T>, index: number]) => UA function to call for each item. |
| comma | optionalbooleanPlace a comma between each element |
| doubleHardline | optionalbooleanPlace two hardlines between each element |
| each | TThe array to iterate over. |
| ender | optionalChildrenText to place at the end of the list when there is at least one item. If set to true, the joiner is used. |
| enderPunctuation | optionalbooleanPlace the join punctuation at the end, but without a line break. |
| hardline | optionalbooleanPlace a hardline ( |
| joiner | optionalChildrenText to place between each element |
| line | optionalbooleanPlace a regular line ( |
| literalline | optionalbooleanPlace a literal line ( |
| semicolon | optionalbooleanPlace a semicolon between each element |
| skipFalsy | optionalbooleanWhether to skip falsy values. By default, falsy values are mapped. However,
when mapping children, it is useful to skip falsy values, as it enables
omitting list elements via patterns like |
| softline | optionalbooleanPlace a softline ( |
| space | optionalbooleanPlace a space between each element |
Remarks
Section titled “Remarks”When the each prop is a reactive (e.g. a reactive array, or ref to an
array), For will automatically update when the array changes. When doing
so, it will attempt to avoid re-rendering items which have not changed. For
example, when appending an item to a reactive array, existing items will not
be re-rendered. Note that presently the implementation is fairly simple -
when making modifications to the middle of an array it likely that every
element after the modification will be rerendered.
Example
Section titled “Example”const items = ["apple", "pear", "plum"];return <For each={items}> {(item) => <>Fruit: {item}</>}</For>See also
Section titled “See also”- mapJoin for mapping arrays to elements outside of JSX templates.