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

# Actions

> User interactions drive state changes, server calls, and UI feedback.

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

An action is what happens when a user interacts. Click a button, submit a form, change a slider: any interaction that matters can carry an action. Actions are how state gets updated, how servers get called, and how the UI gives feedback.

Every interactive component exposes event handlers (`on_click`, `on_change`, `on_submit`) that accept an action, a list of actions, or `None`. When the event fires, the action runs.

For the most common case, syncing an interactive component's value to state, the `name` prop handles it automatically. `Input(name="query")` writes to the `query` key on every keystroke without any explicit action. You reach for actions when you need side effects beyond syncing: showing a toast, calling your server, updating multiple keys at once, or running logic conditionally.

## Two families

Prefab draws a clear line between two kinds of actions.

**Client actions** run entirely in the browser. They execute instantly, require no network, and always succeed. `SetState`, `ToggleState`, `AppendState`, `ShowToast`, `OpenLink`, `SetInterval`: these are your tools for managing UI state and giving immediate feedback. From the user's perspective, they're instantaneous.

**Server actions** cross a network boundary. `CallTool` invokes an MCP tool on your server. `Fetch` makes an HTTP request. They're asynchronous: they take time and they can fail. The UI needs to account for both the happy path and errors.

Most real interactions use both. A "Save" button typically sets a loading flag (client, instant), calls the server (server, async), then shows success or failure feedback (client, instant based on the outcome). The split is a design constraint that keeps the UI responsive and makes the code predictable.

| Client actions | Purpose                                          |
| -------------- | ------------------------------------------------ |
| `SetState`     | Set a state key to a value                       |
| `ToggleState`  | Flip a boolean state key                         |
| `AppendState`  | Add an item to a state array                     |
| `PopState`     | Remove an item from a state array by index       |
| `ShowToast`    | Display a brief notification                     |
| `OpenLink`     | Open a URL                                       |
| `SetInterval`  | Schedule an action to repeat on a timer          |
| `CallHandler`  | Invoke a developer-registered JavaScript handler |

| Server actions  | Purpose                                                             |
| --------------- | ------------------------------------------------------------------- |
| `CallTool`      | Call an MCP tool; result available as `$result` in `on_success`     |
| `Fetch`         | Make an HTTP request; result available as `$result` in `on_success` |
| `SendMessage`   | Send a message to the conversation (MCP hosts only)                 |
| `UpdateContext` | Push structured context to the model (MCP hosts only)               |

## The \$event variable

When an interaction fires, the component emits a value: the slider's current position, the input's current text, the checkbox's checked state. That value is available inside action arguments as `$event`.

Most of the time you don't need `$event` directly, because the `name` prop already syncs the component's value to state automatically. Where `$event` becomes useful is in actions that need to *reference* the emitted value as part of a larger operation, like writing it to a different key, sending it to the server, or using it in a computation:

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.actions import SetState
from prefab_ui.actions.mcp import CallTool

# Write the emitted value to a different state key
Slider(name="volume", on_change=SetState("last_changed_volume", "{{ $event }}"))

# Pass the emitted value to a server call
Input(on_change=CallTool("search", arguments={"q": "{{ $event }}"}))
```

The [Expressions](/concepts/expressions) guide has the full table of what each component emits.

## Chaining actions

A single interaction often needs to do more than one thing. An "Add" button might append an item to a list and then clear the input field. A "Save" button might set a loading flag and then call the server.

To run multiple actions from one event, pass a list. The actions execute in order, and if any action fails, execution stops at that point so a failed server call won't silently proceed to the cleanup steps.

Here's a crew list where the "Add" button chains two actions: `AppendState` pushes the input value onto the array, then `SetState` clears the input field. Both execute before the next render, so the user sees the item appear and the field reset simultaneously.

<ComponentPreview json={{"view":{"cssClass":"gap-3","type":"Column","children":[{"let":{"_loop_7":"{{ $item }}","_loop_7_idx":"{{ $index }}"},"type":"ForEach","key":"crew","children":[{"cssClass":"gap-2 items-center","type":"Row","children":[{"content":"{{ _loop_7 }}","type":"Text"},{"type":"Button","label":"\u00d7","variant":"ghost","size":"sm","disabled":false,"onClick":{"action":"popState","key":"crew","index":"{{ _loop_7_idx }}"}}]}]},{"cssClass":"gap-2","type":"Row","children":[{"name":"new_member","type":"Input","inputType":"text","placeholder":"Crew member name...","disabled":false,"readOnly":false,"required":false},{"type":"Button","label":"Add","variant":"default","size":"default","disabled":false,"onClick":[{"action":"appendState","key":"crew","value":"{{ new_member }}"},{"action":"setState","key":"new_member","value":""}]}]},{"content":"{{ crew | length }} crew members","type":"Muted"}]}}} playground="ZnJvbSBwcmVmYWJfdWkuY29tcG9uZW50cyBpbXBvcnQgQnV0dG9uLCBDb2x1bW4sIEZvckVhY2gsIElucHV0LCBNdXRlZCwgUm93LCBUZXh0CmZyb20gcHJlZmFiX3VpLmFjdGlvbnMgaW1wb3J0IEFwcGVuZFN0YXRlLCBQb3BTdGF0ZSwgU2V0U3RhdGUKZnJvbSBwcmVmYWJfdWkucnggaW1wb3J0IFJ4CgpuZXdfbWVtYmVyID0gUngoIm5ld19tZW1iZXIiKQpjcmV3ID0gUngoImNyZXciKQoKd2l0aCBDb2x1bW4oZ2FwPTMpOgogICAgd2l0aCBGb3JFYWNoKCJjcmV3IikgYXMgaXRlbToKICAgICAgICB3aXRoIFJvdyhnYXA9MiwgYWxpZ249ImNlbnRlciIpOgogICAgICAgICAgICBUZXh0KGl0ZW0pCiAgICAgICAgICAgIEJ1dHRvbigKICAgICAgICAgICAgICAgICLDlyIsCiAgICAgICAgICAgICAgICB2YXJpYW50PSJnaG9zdCIsCiAgICAgICAgICAgICAgICBzaXplPSJzbSIsCiAgICAgICAgICAgICAgICBvbl9jbGljaz1Qb3BTdGF0ZSgiY3JldyIsIGl0ZW0uZ2V0X2luZGV4KCkpCiAgICAgICAgICAgICkKICAgIHdpdGggUm93KGdhcD0yKToKICAgICAgICBJbnB1dChuYW1lPSJuZXdfbWVtYmVyIiwgcGxhY2Vob2xkZXI9IkNyZXcgbWVtYmVyIG5hbWUuLi4iKQogICAgICAgIEJ1dHRvbigKICAgICAgICAgICAgIkFkZCIsCiAgICAgICAgICAgIG9uX2NsaWNrPVsKICAgICAgICAgICAgICAgIEFwcGVuZFN0YXRlKCJjcmV3IiwgbmV3X21lbWJlciksCiAgICAgICAgICAgICAgICBTZXRTdGF0ZSgibmV3X21lbWJlciIsICIiKSwKICAgICAgICAgICAgXQogICAgICAgICkKICAgIE11dGVkKGYie2NyZXcubGVuZ3RoKCl9IGNyZXcgbWVtYmVycyIpCg">
  <CodeGroup>
    ```python Python icon="python" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    from prefab_ui.components import Button, Column, ForEach, Input, Muted, Row, Text
    from prefab_ui.actions import AppendState, PopState, SetState
    from prefab_ui.rx import Rx

    new_member = Rx("new_member")
    crew = Rx("crew")

    with Column(gap=3):
        with ForEach("crew") as item:
            with Row(gap=2, align="center"):
                Text(item)
                Button(
                    "×",
                    variant="ghost",
                    size="sm",
                    on_click=PopState("crew", item.get_index())
                )
        with Row(gap=2):
            Input(name="new_member", placeholder="Crew member name...")
            Button(
                "Add",
                on_click=[
                    AppendState("crew", new_member),
                    SetState("new_member", ""),
                ]
            )
        Muted(f"{crew.length()} crew members")
    ```

    ```json Protocol icon="brackets-curly" theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
    {
      "view": {
        "cssClass": "gap-3",
        "type": "Column",
        "children": [
          {
            "let": {"_loop_7": "{{ $item }}", "_loop_7_idx": "{{ $index }}"},
            "type": "ForEach",
            "key": "crew",
            "children": [
              {
                "cssClass": "gap-2 items-center",
                "type": "Row",
                "children": [
                  {"content": "{{ _loop_7 }}", "type": "Text"},
                  {
                    "type": "Button",
                    "label": "\u00d7",
                    "variant": "ghost",
                    "size": "sm",
                    "disabled": false,
                    "onClick": {"action": "popState", "key": "crew", "index": "{{ _loop_7_idx }}"}
                  }
                ]
              }
            ]
          },
          {
            "cssClass": "gap-2",
            "type": "Row",
            "children": [
              {
                "name": "new_member",
                "type": "Input",
                "inputType": "text",
                "placeholder": "Crew member name...",
                "disabled": false,
                "readOnly": false,
                "required": false
              },
              {
                "type": "Button",
                "label": "Add",
                "variant": "default",
                "size": "default",
                "disabled": false,
                "onClick": [
                  {"action": "appendState", "key": "crew", "value": "{{ new_member }}"},
                  {"action": "setState", "key": "new_member", "value": ""}
                ]
              }
            ]
          },
          {"content": "{{ crew | length }} crew members", "type": "Muted"}
        ]
      }
    }
    ```
  </CodeGroup>
</ComponentPreview>

The list syntax `[action1, action2, ...]` works with any event handler: `on_click`, `on_change`, `on_submit`.

## Callbacks: on\_success and on\_error

Action lists handle the setup: things to do *before* or *alongside* an interaction. Callbacks handle the outcomes: things to do *after* an async action completes.

Every action supports `on_success` and `on_error`. They fire after the action resolves, with the outcome determining which branch runs. Both accept a single action or a list.

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.actions import SetState, ShowToast
from prefab_ui.actions.mcp import CallTool
from prefab_ui.rx import RESULT

Button(
    "Save",
    on_click=CallTool(
        "save_record",
        arguments={"data": Rx("form")},
        on_success=[
            SetState("result", RESULT),
            ShowToast("Saved!", variant="success"),
        ],
        on_error=ShowToast("{{ $error }}", variant="error"),
    ),
)
```

`$result` is available inside `on_success` callbacks; it holds the return value of the action that just completed. `$error` is available inside `on_error` callbacks; it holds the error message from the failed action. These are a matched pair: each exists only within its respective callback scope.

Callbacks can themselves have callbacks, making it possible to chain dependent server calls: the result of the first determines what to fetch next.

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
CallTool(
    "get_user",
    arguments={"id": Rx("user_id")},
    on_success=[
        SetState("user", RESULT),
        CallTool(
            "get_permissions",
            arguments={"role": "{{ user.role }}"},
            on_success=SetState("permissions", RESULT),
        ),
    ],
)
```

## Custom handlers

`CallHandler` invokes a developer-registered JavaScript function client-side. Where `CallTool` crosses the network to your server, `CallHandler` runs instantly in the browser — useful for state transformations that are too complex for expressions but don't need server involvement.

Register handlers via `js_actions` on `PrefabApp`, then reference them by name:

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.actions import CallHandler

Slider(name="infra", on_change=CallHandler("constrainBudget"))
```

The handler receives `{state, event, arguments}` and returns state updates to merge. See [Custom Handlers](/concepts/custom-handlers) for the full API, including custom pipes.

## Common patterns

### Loading state

The combination of lists and callbacks is what makes loading state clean. Set a flag before the server call in the list; clear it in both callback branches so it always resolves, whether the call succeeds or fails:

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.rx import Rx

saving = Rx("saving")

Button(
    saving.then("Saving...", "Save"),
    disabled=saving,
    on_click=[
        SetState("saving", True),
        CallTool(
            "save",
            arguments={"data": Rx("form")},
            on_success=[
                SetState("saving", False),
                ShowToast("Saved!", variant="success"),
            ],
            on_error=[
                SetState("saving", False),
                ShowToast("{{ $error }}", variant="error"),
            ],
        ),
    ],
)
```

The button label switches and disables while the call is in flight. Both branches clear the flag, so the button always returns to its normal state.

### Populating results

For search and filter patterns, use `RESULT` in an `on_success` callback to write the tool's response directly into state, then have a `ForEach` or `Slot` display it:

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
from prefab_ui.actions import SetState
from prefab_ui.actions.mcp import CallTool
from prefab_ui.components import Button, Column, ForEach, Input, Text
from prefab_ui.rx import RESULT

with Column(gap=3):
    query = Input(name="q", placeholder="Search...")
    Button(
        "Search",
        on_click=CallTool(
            "search",
            arguments={"q": query.rx},
            on_success=SetState("results", RESULT),
        ),
    )
    with ForEach("results"):
        Text("{{ name }}")
```

When the tool returns, `RESULT` holds the response data. `SetState` writes it to `results`, and the `ForEach` re-renders with whatever came back.

### Optimistic updates

For interactions where you're confident the server will succeed, update state immediately and let the server confirm in the background. Revert on failure:

```python theme={"theme":{"light":"snazzy-light","dark":"dark-plus"}}
with ForEach("todos"):
    Checkbox(
        name="todos.{{ $index }}.done",
        on_change=[
            SetState("todos.{{ $index }}.done", "{{ $event }}"),
            CallTool(
                "persist_todo",
                arguments={"id": "{{ $item.id }}", "done": "{{ $event }}"},
                on_error=[
                    SetState("todos.{{ $index }}.done", "{{ !$event }}"),
                    ShowToast("Failed to save", variant="error"),
                ],
            ),
        ],
    )
```

The checkbox updates instantly. If the server fails, the `on_error` branch flips it back and shows a toast.
