Skip to content

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>
children(…args: […ForCallbackArgs<T>, index: number]) => UA function to call for each item.
commaoptional booleanPlace a comma between each element
doubleHardlineoptional booleanPlace two hardlines between each element
eachTThe array to iterate over.
enderoptional ChildrenText to place at the end of the list when there is at least one item. If set to true, the joiner is used.
enderPunctuationoptional booleanPlace the join punctuation at the end, without a line break. The punctuation is emitted unconditionally in flat and broken modes. To add trailing punctuation only in broken mode, use ender with <ifBreak> inside a <group> with a non-hardline joiner (e.g. line):
<group>
  <List comma line ender={<ifBreak>,</ifBreak>}>...</List>
</group>
hardlineoptional booleanPlace a hardline (<hbr />) between each element
joineroptional ChildrenText to place between each element
lineoptional booleanPlace a regular line (<br />) between each element
literallineoptional booleanPlace a literal line (<lbr />) between each element
semicolonoptional booleanPlace a semicolon between each element
skipFalsyoptional booleanWhether 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 {condition && <ListItem />}.
softlineoptional booleanPlace a softline (<sbr />) between each element
spaceoptional booleanPlace a space between each element

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.

const items = ["apple", "pear", "plum"];
return <For each={items}>
  {(item) => <>Fruit: {item}</>}
</For>
  • mapJoin for mapping arrays to elements outside of JSX templates.