Skip to main content

Component | For Each

note

Introduced in SDK v0.25.3.

Map a list of items to a list of components.

page.add(() =>
ui.forEach(["1", "2", "3"], (num) => ui.text(num))
);

API reference

Function signature

type Component = ReturnType<typeof ui.textInput> | ReturnType<typeof ui.button> | /* more components */ | ReturnType<typeof ui.stack>

ui.forEach<T>(
items: Array<T>,
generator: (item: T, index: number) => Component
options?: Partial<{
direction: "vertical" | "horizontal" | "vertical-reverse" | "horizontal-reverse",
justify: "start" | "end" | "center" | "between" | "around" | "evenly",
align: "start" | "end" | "center" | "baseline" | "stretch",
spacing: string,
style: Style
}>
)

Parameters

items

required Array<T>
The array of items to map to components.

generator

required function(item: T, index: number): Component
A function that takes an item and index, and returns a component.

properties.direction

optional string literal

The direction in which the children should be arranged. Defaults to "vertical".

Options:

  • "vertical": Arrange children vertically (top to bottom)
  • "horizontal": Arrange children horizontally (left to right)
  • "vertical-reverse": Arrange children vertically in reverse order (bottom to top)
  • "horizontal-reverse": Arrange children horizontally in reverse order (right to left)

properties.justify

optional string literal

Determine how the children are justified along the main axis of the container. Defaults to "start".

Options:

  • "start": Align children to the start of the container
  • "end": Align children to the end of the container
  • "center": Center children within the container
  • "between": Distribute children evenly with equal space between them
  • "around": Distribute children evenly with equal space around them
  • "evenly": Distribute children evenly with equal space between and around them

properties.align

optional string literal

Determine how the children are aligned along the cross axis of the container. Defaults to "start".

Options:

  • "start": Align children to the start of the container
  • "end": Align children to the end of the container
  • "center": Center children within the container
  • "baseline": Align children along their baseline
  • "stretch": Stretch children to fill the container

properties.spacing

optional string

Set the spacing between child components. Defaults to "16px".

Options include a variety of preset values like "0px", "2px", "4px", ..., "160px", or any custom string ending with "px".


properties.style

optional Style

Directly style the underlying div container element using CSS. See the styling guide for more details on available style properties.

For example, you could set a border around the stack to create a card component.