> ## 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.

# Dynamic List

> Add and remove items with ForEach, AppendState, and PopState.

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>;
};

A task list you can add to and remove from. `ForEach` renders the items, `AppendState` adds new ones, and `PopState` removes by index. The item count updates reactively via the `length` pipe.

<ComponentPreview json={{"view":{"cssClass":"pf-app-root max-w-none p-0","type":"Div","children":[{"cssClass":"w-full","type":"Card","children":[{"type":"CardHeader","children":[{"type":"CardTitle","content":"Task List"},{"content":"{{ tasks | length }} items","type":"Muted"}]},{"type":"CardContent","children":[{"cssClass":"gap-3","type":"Column","children":[{"cssClass":"gap-2","type":"Row","children":[{"name":"new_task","type":"Input","inputType":"text","placeholder":"Add a task...","disabled":false,"readOnly":false,"required":false},{"type":"Button","label":"Add","variant":"default","size":"default","disabled":false,"onClick":[{"action":"appendState","key":"tasks","value":"{{ new_task }}"},{"action":"setState","key":"new_task","value":""}]}]},{"type":"Separator","orientation":"horizontal"},{"let":{"_loop_9":"{{ $item }}","_loop_9_idx":"{{ $index }}"},"type":"ForEach","key":"tasks","children":[{"cssClass":"gap-2 items-center justify-between","type":"Row","children":[{"content":"{{ $item }}","type":"Text"},{"type":"Button","label":"Remove","variant":"outline","size":"sm","disabled":false,"onClick":{"action":"popState","key":"tasks","index":"{{ $index }}"}}]}]}]}]},{"type":"CardFooter","children":[{"type":"Button","label":"Clear All","variant":"destructive","size":"default","disabled":false,"onClick":{"action":"setState","key":"tasks","value":[]}}]}]}]},"state":{"tasks":["Buy towel","Learn Python","Don't panic"],"new_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dGFzay4uLiIpCiAgICAgICAgICAgICAgICAgICAgQnV0dG9uKAogICAgICAgICAgICAgICAgICAgICAgICAiQWRkIiwKICAgICAgICAgICAgICAgICAgICAgICAgb25fY2xpY2s9WwogICAgICAgICAgICAgICAgICAgICAgICAgICAgQXBwZW5kU3RhdGUoInRhc2tzIiwgbmV3X3Rhc2spLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgU2V0U3RhdGUoIm5ld190YXNrIiwgIiIpLAogICAgICAgICAgICAgICAgICAgICAgICBdLAogICAgICAgICAgICAgICAgICAgICkKICAgICAgICAgICAgICAgIFNlcGFyYXRvcigpCiAgICAgICAgICAgICAgICB3aXRoIEZvckVhY2godGFza3MpOgogICAgICAgICAgICAgICAgICAgIHdpdGggUm93KGdhcD0yLCBhbGlnbj0iY2VudGVyIiwgY3NzX2NsYXNzPSJqdXN0aWZ5LWJldHdlZW4iKToKICAgICAgICAgICAgICAgICAgICAgICAgVGV4dChJVEVNKQogICAgICAgICAgICAgICAgICAgICAgICBCdXR0b24oCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAiUmVtb3ZlIiwgdmFyaWFudD0ib3V0bGluZSIsIHNpemU9InNtIiwKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG9uX2NsaWNrPVBvcFN0YXRlKCJ0YXNrcyIsIGluZGV4PUlOREVYKSwKICAgICAgICAgICAgICAgICAgICAgICAgKQogICAgICAgIHdpdGggQ2FyZEZvb3RlcigpOgogICAgICAgICAgICBCdXR0b24oIkNsZWFyIEFsbCIsIHZhcmlhbnQ9ImRlc3RydWN0aXZlIiwgb25fY2xpY2s9U2V0U3RhdGUoInRhc2tzIiwgW10pKQo">
  <CodeGroup>
    ```python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui import PrefabApp
    from prefab_ui.actions import AppendState, PopState, SetState
    from prefab_ui.components import (
        Button, Card, CardContent, CardFooter, CardHeader, CardTitle,
        Column, Input, Muted, Row, Separator, Text,
    )
    from prefab_ui.components.control_flow import ForEach
    from prefab_ui.rx import INDEX, ITEM, Rx

    new_task = Rx("new_task")
    tasks = Rx("tasks")

    with PrefabApp(
        state={"tasks": ["Buy towel", "Learn Python", "Don't panic"], "new_task": ""},
        css_class="max-w-none p-0",
    ):
        with Card(css_class="w-full"):
            with CardHeader():
                CardTitle("Task List")
                Muted(f"{tasks.length()} items")
            with CardContent():
                with Column(gap=3):
                    with Row(gap=2):
                        Input(name="new_task", placeholder="Add a task...")
                        Button(
                            "Add",
                            on_click=[
                                AppendState("tasks", new_task),
                                SetState("new_task", ""),
                            ],
                        )
                    Separator()
                    with ForEach(tasks):
                        with Row(gap=2, align="center", css_class="justify-between"):
                            Text(ITEM)
                            Button(
                                "Remove", variant="outline", size="sm",
                                on_click=PopState("tasks", index=INDEX),
                            )
            with CardFooter():
                Button("Clear All", variant="destructive", on_click=SetState("tasks", []))
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "pf-app-root max-w-none p-0",
        "type": "Div",
        "children": [
          {
            "cssClass": "w-full",
            "type": "Card",
            "children": [
              {
                "type": "CardHeader",
                "children": [
                  {"type": "CardTitle", "content": "Task List"},
                  {"content": "{{ tasks | length }} items", "type": "Muted"}
                ]
              },
              {
                "type": "CardContent",
                "children": [
                  {
                    "cssClass": "gap-3",
                    "type": "Column",
                    "children": [
                      {
                        "cssClass": "gap-2",
                        "type": "Row",
                        "children": [
                          {
                            "name": "new_task",
                            "type": "Input",
                            "inputType": "text",
                            "placeholder": "Add a task...",
                            "disabled": false,
                            "readOnly": false,
                            "required": false
                          },
                          {
                            "type": "Button",
                            "label": "Add",
                            "variant": "default",
                            "size": "default",
                            "disabled": false,
                            "onClick": [
                              {"action": "appendState", "key": "tasks", "value": "{{ new_task }}"},
                              {"action": "setState", "key": "new_task", "value": ""}
                            ]
                          }
                        ]
                      },
                      {"type": "Separator", "orientation": "horizontal"},
                      {
                        "let": {"_loop_9": "{{ $item }}", "_loop_9_idx": "{{ $index }}"},
                        "type": "ForEach",
                        "key": "tasks",
                        "children": [
                          {
                            "cssClass": "gap-2 items-center justify-between",
                            "type": "Row",
                            "children": [
                              {"content": "{{ $item }}", "type": "Text"},
                              {
                                "type": "Button",
                                "label": "Remove",
                                "variant": "outline",
                                "size": "sm",
                                "disabled": false,
                                "onClick": {"action": "popState", "key": "tasks", "index": "{{ $index }}"}
                              }
                            ]
                          }
                        ]
                      }
                    ]
                  }
                ]
              },
              {
                "type": "CardFooter",
                "children": [
                  {
                    "type": "Button",
                    "label": "Clear All",
                    "variant": "destructive",
                    "size": "default",
                    "disabled": false,
                    "onClick": {"action": "setState", "key": "tasks", "value": []}
                  }
                ]
              }
            ]
          }
        ]
      },
      "state": {"tasks": ["Buy towel", "Learn Python", "Don't panic"], "new_task": ""}
    }
    ```
  </CodeGroup>
</ComponentPreview>

## How It Works

The list lives in [state](/concepts/state). `PrefabApp` seeds two keys: `tasks`, a list of strings, and `new_task`, the text currently in the input. Everything on screen is a function of those two values, and every button mutates them through client-side [actions](/concepts/actions) — there is no server in the loop.

The input and the add button form a small write/clear cycle. `Input(name="new_task")` two-way binds to the `new_task` state key, so typing keeps that key current. The button fires a *list* of actions in order: `AppendState("tasks", new_task)` pushes the input's current value onto the `tasks` list, then `SetState("new_task", "")` empties the box so it's ready for the next entry. Passing `new_task = Rx("new_task")` as the value (rather than a literal) means the action reads live state at click time, appending whatever the user actually typed.

The list itself is rendered with [`ForEach`](/components/foreach). `with ForEach(tasks):` iterates the `tasks` state key and re-runs whenever it changes — add or remove an item and the rendered rows update with no manual bookkeeping. Inside the loop, `ITEM` is the current element's value and `INDEX` is its position. `Text(ITEM)` shows the task, and the remove button calls `PopState("tasks", index=INDEX)` to delete exactly that row by index. Because `ForEach` supplies a stable `INDEX` per row, each Remove button targets the right element even as the list shifts.

The footer count, `Muted(f"{tasks.length()} items")`, is a [reactive expression](/concepts/expressions): `tasks.length()` compiles to a `length` pipe the renderer recomputes on every state change, so the number tracks the list without any explicit update. `Clear All` is the simplest mutation of all — `SetState("tasks", [])` replaces the whole list with an empty one, and `ForEach`, the count, and the buttons all collapse accordingly.
