> ## Documentation Index
> Fetch the complete documentation index at: https://prefab.prefect.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ForEach

> Repeat components for each item in a data list.

export const ComponentPreview = ({json, height, resizable, bare, hideJson, playground, children}) => {
  const hostRef = React.useRef(null);
  const handleRef = React.useRef(null);
  const cardRef = React.useRef(null);
  const containerRef = React.useRef(null);
  const [previewWidth, setPreviewWidth] = React.useState(null);
  const [isDragging, setIsDragging] = React.useState(false);
  const [playgroundHref, setPlaygroundHref] = React.useState(null);
  React.useEffect(function () {
    if (!playground) return;
    var isLocal = location.hostname === "localhost" || location.hostname === "127.0.0.1";
    var path = isLocal ? "/playground" : "/docs/playground";
    var url = new URL(path, window.location.href);
    url.hash = "code=" + playground;
    setPlaygroundHref(url.href);
  }, [playground]);
  var jsonStr = typeof json === "string" ? json : JSON.stringify(json, null, 2);
  React.useEffect(function () {
    if (cardRef.current) {
      var card = cardRef.current.closest(".group");
      if (card) card.classList.remove("group");
    }
    var host = hostRef.current;
    if (!host || !json) return;
    function mount() {
      if (!window.__prefab || !host) return;
      var dark = document.documentElement.classList.contains("dark");
      handleRef.current = window.__prefab.mountPreview(host, jsonStr, {
        dark: dark
      });
      if (bare && host.shadowRoot) {
        var m = host.shadowRoot.querySelector("[data-prefab-mount]");
        if (m) m.style.background = "transparent";
      }
    }
    if (window.__prefab) {
      mount();
    } else {
      if (!window.__prefabLoading) {
        if (!window.__prefabReady) {
          var s = document.createElement("script");
          s.src = "/renderer.js";
          document.head.appendChild(s);
        }
        window.__prefabLoading = new Promise(function (resolve) {
          function check() {
            if (window.__prefabReady) {
              window.__prefabReady.then(resolve);
            } else {
              setTimeout(check, 10);
            }
          }
          check();
        });
      }
      window.__prefabLoading.then(mount);
    }
    var observer = new MutationObserver(function () {
      var dark = document.documentElement.classList.contains("dark");
      if (handleRef.current) handleRef.current.setDark(dark);
    });
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class"]
    });
    return function () {
      observer.disconnect();
      if (handleRef.current) {
        handleRef.current.unmount();
        handleRef.current = null;
      }
    };
  }, [json]);
  function startResize(e) {
    e.preventDefault();
    var startX = e.clientX;
    var startW = hostRef.current ? hostRef.current.offsetWidth : 0;
    var maxW = containerRef.current ? containerRef.current.offsetWidth - 10 : startW;
    setIsDragging(true);
    function onMove(ev) {
      var w = Math.max(260, Math.min(startW + (ev.clientX - startX), maxW));
      setPreviewWidth(w);
    }
    function onUp() {
      document.removeEventListener("mousemove", onMove);
      document.removeEventListener("mouseup", onUp);
      setIsDragging(false);
    }
    document.addEventListener("mousemove", onMove);
    document.addEventListener("mouseup", onUp);
  }
  if (bare) {
    return <div ref={hostRef} />;
  }
  if (resizable) {
    return <Card>
        <div ref={cardRef} />
        <div ref={containerRef} style={{
      display: "flex",
      position: "relative",
      overflow: "hidden"
    }}>
          <div ref={hostRef} style={{
      flex: previewWidth ? "none" : 1,
      width: previewWidth ? previewWidth + "px" : undefined,
      minWidth: 0
    }} />
          <div onMouseDown={startResize} style={{
      width: "14px",
      flexShrink: 0,
      cursor: "col-resize",
      background: isDragging ? "var(--border, #e5e7eb)" : "color-mix(in srgb, var(--border, #e5e7eb) 40%, transparent)",
      display: "flex",
      alignItems: "center",
      justifyContent: "center",
      transition: "background 0.15s",
      userSelect: "none"
    }}>
            <svg width="6" height="24" style={{
      opacity: 0.5
    }}>
              <circle cx="2" cy="4" r="1.2" fill="currentColor" />
              <circle cx="2" cy="9" r="1.2" fill="currentColor" />
              <circle cx="2" cy="14" r="1.2" fill="currentColor" />
              <circle cx="2" cy="19" r="1.2" fill="currentColor" />
              <circle cx="5" cy="4" r="1.2" fill="currentColor" />
              <circle cx="5" cy="9" r="1.2" fill="currentColor" />
              <circle cx="5" cy="14" r="1.2" fill="currentColor" />
              <circle cx="5" cy="19" r="1.2" fill="currentColor" />
            </svg>
          </div>
          <div style={{
      flex: previewWidth ? 1 : "0 0 24px",
      minWidth: "24px",
      background: "repeating-linear-gradient(-45deg, transparent, transparent 3px, var(--border, #e5e7eb) 3px, var(--border, #e5e7eb) 4px)",
      opacity: 0.4
    }} />
          {previewWidth && <div style={{
      position: "absolute",
      bottom: "8px",
      right: "8px",
      background: "var(--background, white)",
      border: "1px solid var(--border, #e5e7eb)",
      borderRadius: "4px",
      padding: "1px 6px",
      fontSize: "11px",
      fontFamily: "monospace",
      color: "var(--muted-foreground, #6b7280)",
      pointerEvents: "none"
    }}>
              {previewWidth}px
            </div>}
        </div>
        {playgroundHref && <div style={{
      display: "flex",
      justifyContent: "flex-end",
      padding: "4px 0 8px"
    }}>
            <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
      fontSize: "11px",
      opacity: 0.4,
      textDecoration: "none",
      textDecorationLine: "none",
      borderBottom: "none",
      boxShadow: "none",
      display: "inline-flex",
      alignItems: "center",
      gap: "3px",
      transition: "opacity 0.15s",
      fontWeight: 400
    }} onMouseOver={function (e) {
      e.currentTarget.style.opacity = "0.6";
    }} onMouseOut={function (e) {
      e.currentTarget.style.opacity = "0.4";
    }}>
              Edit in Playground
              <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
            </a>
          </div>}
        <div style={{
      marginBottom: "-2rem"
    }}>{children}</div>
      </Card>;
  }
  return <Card>
      <div ref={cardRef} />
      <div ref={hostRef} />
      {playgroundHref && <div style={{
    display: "flex",
    justifyContent: "flex-end",
    padding: "4px 0 8px"
  }}>
          <a href={playgroundHref} target="_blank" rel="noopener noreferrer" style={{
    fontSize: "11px",
    opacity: 0.4,
    textDecoration: "none",
    textDecorationLine: "none",
    borderBottom: "none",
    boxShadow: "none",
    display: "inline-flex",
    alignItems: "center",
    gap: "3px",
    transition: "opacity 0.15s",
    fontWeight: 400
  }} onMouseOver={function (e) {
    e.currentTarget.style.opacity = "0.6";
  }} onMouseOut={function (e) {
    e.currentTarget.style.opacity = "0.4";
  }}>
            Edit in Playground
            <svg xmlns="http://www.w3.org/2000/svg" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M15 3h6v6" /><path d="M10 14 21 3" /><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6" /></svg>
          </a>
        </div>}
      <div style={{
    marginBottom: "-2rem"
  }}>{children}</div>
    </Card>;
};

Given a list of data, `ForEach` renders its children once per item — a card for each user, a badge for each tag, a row for each result. Python's context manager gives you a handle to the current item, and attribute access chains naturally into dot-path expressions, so `member.name` in Python becomes `{{ _loop_1.name }}` in the rendered output.

## Basic Usage

Pass `ForEach` a state key that contains a list. The `as` variable is a reactive reference to the current item — use dot notation to reach into fields, embed it in f-strings, or pass it directly to components that accept string values.

<ComponentPreview json={{"view":{"cssClass":"gap-2","type":"Column","children":[{"let":{"_loop_3":"{{ $item }}","_loop_3_idx":"{{ $index }}"},"type":"ForEach","key":"crew","children":[{"type":"Card","children":[{"type":"CardHeader","children":[{"cssClass":"gap-2 items-center","type":"Row","children":[{"type":"CardTitle","content":"{{ _loop_3.name }}"},{"type":"Badge","label":"{{ _loop_3.designation }}","variant":"secondary"}]},{"type":"CardDescription","content":"{{ _loop_3.email }}"}]}]}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgKAogICAgQ2FyZCwKICAgIENhcmRIZWFkZXIsCiAgICBDYXJkVGl0bGUsCiAgICBDYXJkRGVzY3JpcHRpb24sCiAgICBCYWRnZSwKICAgIENvbHVtbiwKICAgIFJvdywKKQpmcm9tIHByZWZhYl91aS5jb21wb25lbnRzLmNvbnRyb2xfZmxvdyBpbXBvcnQgRm9yRWFjaAoKCndpdGggQ29sdW1uKGdhcD0yKToKICAgIHdpdGggRm9yRWFjaCgiY3JldyIpIGFzIG1lbWJlcjoKICAgICAgICB3aXRoIENhcmQoKToKICAgICAgICAgICAgd2l0aCBDYXJkSGVhZGVyKCk6CiAgICAgICAgICAgICAgICB3aXRoIFJvdyhnYXA9MiwgYWxpZ249ImNlbnRlciIpOgogICAgICAgICAgICAgICAgICAgIENhcmRUaXRsZShtZW1iZXIubmFtZSkKICAgICAgICAgICAgICAgICAgICBCYWRnZShtZW1iZXIuZGVzaWduYXRpb24sIHZhcmlhbnQ9InNlY29uZGFyeSIpCiAgICAgICAgICAgICAgICBDYXJkRGVzY3JpcHRpb24obWVtYmVyLmVtYWlsKQo">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import (
        Card,
        CardHeader,
        CardTitle,
        CardDescription,
        Badge,
        Column,
        Row,
    )
    from prefab_ui.components.control_flow import ForEach


    with Column(gap=2):
        with ForEach("crew") as member:
            with Card():
                with CardHeader():
                    with Row(gap=2, align="center"):
                        CardTitle(member.name)
                        Badge(member.designation, variant="secondary")
                    CardDescription(member.email)
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-2",
        "type": "Column",
        "children": [
          {
            "let": {"_loop_3": "{{ $item }}", "_loop_3_idx": "{{ $index }}"},
            "type": "ForEach",
            "key": "crew",
            "children": [
              {
                "type": "Card",
                "children": [
                  {
                    "type": "CardHeader",
                    "children": [
                      {
                        "cssClass": "gap-2 items-center",
                        "type": "Row",
                        "children": [
                          {"type": "CardTitle", "content": "{{ _loop_3.name }}"},
                          {"type": "Badge", "label": "{{ _loop_3.designation }}", "variant": "secondary"}
                        ]
                      },
                      {"type": "CardDescription", "content": "{{ _loop_3.email }}"}
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

When items are simple values (strings, numbers) rather than objects, the context variable represents the value directly — no dot access needed.

<ComponentPreview json={{"view":{"cssClass":"gap-2","type":"Row","children":[{"let":{"_loop_4":"{{ $item }}","_loop_4_idx":"{{ $index }}"},"type":"ForEach","key":"tags","children":[{"type":"Badge","label":"{{ _loop_4 }}","variant":"secondary"}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQmFkZ2UsIFJvdwpmcm9tIHByZWZhYl91aS5jb21wb25lbnRzLmNvbnRyb2xfZmxvdyBpbXBvcnQgRm9yRWFjaAoKCndpdGggUm93KGdhcD0yKToKICAgIHdpdGggRm9yRWFjaCgidGFncyIpIGFzIHRhZzoKICAgICAgICBCYWRnZSh0YWcsIHZhcmlhbnQ9InNlY29uZGFyeSIpCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Badge, Row
    from prefab_ui.components.control_flow import ForEach


    with Row(gap=2):
        with ForEach("tags") as tag:
            Badge(tag, variant="secondary")
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-2",
        "type": "Row",
        "children": [
          {
            "let": {"_loop_4": "{{ $item }}", "_loop_4_idx": "{{ $index }}"},
            "type": "ForEach",
            "key": "tags",
            "children": [{"type": "Badge", "label": "{{ _loop_4 }}", "variant": "secondary"}]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

Dot notation chains through nested objects the same way you'd expect — `project.owner.name` reaches into `{"owner": {"name": "..."}}` without any special syntax.

<ComponentPreview json={{"view":{"cssClass":"gap-2","type":"Column","children":[{"let":{"_loop_5":"{{ $item }}","_loop_5_idx":"{{ $index }}"},"type":"ForEach","key":"projects","children":[{"type":"Card","children":[{"type":"CardHeader","children":[{"type":"CardTitle","content":"{{ _loop_5.name }}"},{"type":"CardDescription","content":"Owner: {{ _loop_5.owner.name }}"}]}]}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgKAogICAgQ2FyZCwKICAgIENhcmRIZWFkZXIsCiAgICBDYXJkVGl0bGUsCiAgICBDYXJkRGVzY3JpcHRpb24sCiAgICBDb2x1bW4sCikKZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cy5jb250cm9sX2Zsb3cgaW1wb3J0IEZvckVhY2gKCgp3aXRoIENvbHVtbihnYXA9Mik6CiAgICB3aXRoIEZvckVhY2goInByb2plY3RzIikgYXMgcHJvamVjdDoKICAgICAgICB3aXRoIENhcmQoKToKICAgICAgICAgICAgd2l0aCBDYXJkSGVhZGVyKCk6CiAgICAgICAgICAgICAgICBDYXJkVGl0bGUocHJvamVjdC5uYW1lKQogICAgICAgICAgICAgICAgQ2FyZERlc2NyaXB0aW9uKGYiT3duZXI6IHtwcm9qZWN0Lm93bmVyLm5hbWV9IikK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import (
        Card,
        CardHeader,
        CardTitle,
        CardDescription,
        Column,
    )
    from prefab_ui.components.control_flow import ForEach


    with Column(gap=2):
        with ForEach("projects") as project:
            with Card():
                with CardHeader():
                    CardTitle(project.name)
                    CardDescription(f"Owner: {project.owner.name}")
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-2",
        "type": "Column",
        "children": [
          {
            "let": {"_loop_5": "{{ $item }}", "_loop_5_idx": "{{ $index }}"},
            "type": "ForEach",
            "key": "projects",
            "children": [
              {
                "type": "Card",
                "children": [
                  {
                    "type": "CardHeader",
                    "children": [
                      {"type": "CardTitle", "content": "{{ _loop_5.name }}"},
                      {"type": "CardDescription", "content": "Owner: {{ _loop_5.owner.name }}"}
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

## Destructuring

The `as` clause supports tuple destructuring — `as (i, item)` — giving you both the zero-based iteration index and the item, matching Python's `enumerate` convention.

The index is valuable for two things: displaying position numbers, and constructing state paths that target specific items. When an action like `SetState` needs to modify one particular item in a list, you need its index to build the path.

<ComponentPreview json={{"view":{"cssClass":"gap-1 w-fit mx-auto","type":"Column","children":[{"content":"The Plan:","type":"H4"},{"let":{"_loop_6":"{{ $item }}","_loop_6_idx":"{{ $index }}"},"type":"ForEach","key":"steps","children":[{"content":"{{ _loop_6_idx + 1 }}. {{ _loop_6 }}","type":"Text"}]}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQ29sdW1uLCBUZXh0LCBINApmcm9tIHByZWZhYl91aS5jb21wb25lbnRzLmNvbnRyb2xfZmxvdyBpbXBvcnQgRm9yRWFjaAoKCndpdGggQ29sdW1uKGdhcD0xLCBjc3NfY2xhc3M9InctZml0IG14LWF1dG8iKToKICAgIEg0KCJUaGUgUGxhbjoiKQogICAgd2l0aCBGb3JFYWNoKCJzdGVwcyIpIGFzIChpLCBzdGVwKToKICAgICAgICBUZXh0KGYie2kgKyAxfS4ge3N0ZXB9IikK">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Column, Text, H4
    from prefab_ui.components.control_flow import ForEach


    with Column(gap=1, css_class="w-fit mx-auto"):
        H4("The Plan:")
        with ForEach("steps") as (i, step):
            Text(f"{i + 1}. {step}")
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-1 w-fit mx-auto",
        "type": "Column",
        "children": [
          {"content": "The Plan:", "type": "H4"},
          {
            "let": {"_loop_6": "{{ $item }}", "_loop_6_idx": "{{ $index }}"},
            "type": "ForEach",
            "key": "steps",
            "children": [{"content": "{{ _loop_6_idx + 1 }}. {{ _loop_6 }}", "type": "Text"}]
          }
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

Use the simpler `as item` form when you only need the item. `as (i, item)` is worth reaching for when you need positional display or indexed state mutations like `SetState(f"items.{i}.done", True)`.

## Nested Loops

When ForEach loops nest, each level automatically captures `$item` and `$index` into uniquely scoped `let` bindings. This means the outer loop's variables survive even after the inner loop introduces its own — no manual scoping required.

Destructure both levels to get named handles. The outer group's index (`gi`) becomes part of the inner loop's key path, threading the two loops together:

```python Nested ForEach theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.components import Checkbox, Column, Input
from prefab_ui.components.control_flow import ForEach

with ForEach("groups") as (gi, group):
    with Column():
        Input(name=f"groups.{gi}.name")
        with ForEach(f"groups.{gi}.todos") as (ti, todo):
            Checkbox(name=f"groups.{gi}.todos.{ti}.done")
```

`gi` and `group` reference the outer loop's index and item; `ti` and `todo` reference the inner loop's. Both sets remain valid inside the inner body because each ForEach stores them in separate `let` scopes under the hood — `group` resolves to `_loop_1` (bound to the outer `$item`), while `todo` resolves to `_loop_2` (bound to the inner `$item`).

The [Todo List](/examples/todo) example is a full nested-loop application with inline editing, conditional visibility, and indexed state mutations across both loop levels.

## API Reference

<Card icon="code" title="ForEach Parameters">
  <ParamField body="key" type="str" required>
    The data field containing the list to iterate over. Can be passed as a positional argument.
  </ParamField>

  <ParamField body="css_class" type="str | None" default="None">
    Additional Tailwind CSS classes for the wrapper element.
  </ParamField>
</Card>

## Protocol Reference

```json ForEach theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
{
  "type": "ForEach",
  "id?": "string",
  "onMount?": "any",
  "children?": "[Component]",
  "let?": "object",
  "key": "string (required)",
  "cssClass?": "string"
}
```

For the complete protocol schema, see [ForEach](/protocol/for-each).
