---
title: "Lifecycle"
description: "Understand how Radiant connects hosts, catches up attributes, renders views, and cleans up on disconnect."
order: 3
---

# Lifecycle

This page describes the client lifecycle of a `RadiantElement`. Radiant follows the browser's custom-element lifecycle and adds a reactive connection cycle. The useful distinction is timing: `registerConnectedCallback()` runs synchronously at the start of `connectedCallback()`, while `onConnected()` runs after first-connect attribute catch-up and the initial render or hydration work.

## Connection sequence

This is the normal connection path, not every internal branch. A host that does not override `render()` keeps its authored light DOM instead of entering the initial-view step. On reconnect, `onConnected()` runs again after Radiant restores the host's reactive connection. `registerConnectedCallback()` is intentionally not shown as a separate application step: it is an early, low-level hook inside `connectedCallback()`.

```mermaid
flowchart TD
  Connect[Browser calls connectedCallback] --> Early[Registered connection callbacks run]
  Early --> Queue[Radiant queues the connection microtask]
  Queue --> Catchup[Authored attributes catch up]
  Catchup --> Owns{render is overridden?}
  Owns -- yes --> View[Hydrate or render the initial view]
  Owns -- no --> Keep[Keep the authored light DOM]
  View --> Paint[Flush bindTo paint]
  Keep --> Paint
  Paint --> Connected[onConnected]
  Connected --> Updated[updated]
```

## Reactive updates

Connection is only the first pass. After a host is connected, a property or observed attribute can trigger `@bindTo` paint, an `@onUpdated()` callback, and, for render-owning hosts, a scheduled `update()`.

```mermaid
flowchart LR
  Change[Property or observed attribute changes] --> Paint[bindTo paints existing DOM]
  Change --> Callback[onUpdated callback runs]
  Change --> Owns{Host owns render?}
  Owns -- yes --> Update[Scheduled update commits the view]
  Owns -- no --> Done[No render step]
  Update --> Updated[updated]
```

The branches are independent. Use `@bindTo` to copy a field onto existing DOM. Use `@onUpdated()` for a procedure. Use JSX bindings and the update cycle for DOM your host owns in `render()`. A plain `RadiantElement` that keeps authored children does not enter the render branch unless it overrides `render()`. During first-connect `@prop` synchronization, `@onUpdated()` and `@bindTo` can run before `onConnected()`. `@bindTo` also flushes after catch-up (and the initial hydrate/update) so parent-authored `data-ref` nodes are visible.

## Disconnect and reconnect

Disconnect tears down connection-scoped resources. The same instance can later reconnect.

```mermaid
stateDiagram-v2
  [*] --> Connected: connectedCallback and onConnected
  Connected --> Disconnected: disconnectedCallback tears down resources
  Disconnected --> Connected: connectedCallback, then onConnected again
```

## What each hook is for

| Hook or API | Runs | Use it for |
| --- | --- | --- |
| `constructor()` | During element construction | Initialize instance fields. Do not assume authored attributes or child DOM are available. |
| `connectedCallback()` | When the browser connects the element | Synchronous native setup that must begin immediately, such as installing event listeners or a `MutationObserver`. Call `super.connectedCallback()` first when overriding it. |
| `registerConnectedCallback()` | Synchronously from `connectedCallback()`, on each connection | Low-level decorator and host-integration plumbing. It runs before attribute catch-up, so application code should not use it when it needs final property values. |
| `attributeChangedCallback()` | When an observed attribute changes after the host is ready | Radiant's internal attribute-to-property bridge. Most components should not override it; authored first-connect attributes are replayed during the connection microtask. |
| `onConnected()` | In the connection microtask, after catch-up and initial render or hydration | Post-sync setup that needs reactive properties or rendered `data-ref` elements. It runs again after reconnect. A server-rendered host nested in another host's view runs it once its parent hands it its children and it hydrates. |
| `updated(changed)` | After each update cycle, once `@onUpdated` ran and any pending render committed | Work that needs the committed DOM, such as focus or measuring. State written here schedules another cycle. |
| `updateComplete` | Resolves when no member change or render is pending; rejects when the cycle throws | Tests and callers that must wait for the cycle, including the first connect render. |
| `@bindTo(...)` | After catch-up and on every write of the named field | Copy the field onto host or descendant attributes, properties, or text. |
| `createResource(...)` | In the connection microtask, after `@bindTo` and before `onConnected()`; client only | Host-owned async data that should fetch with authored prop values. |
| `@onUpdated(...)` | Once per update cycle when a watched member changed, before the render commits, including the initial `@prop` sync | Procedures: timers, joined ARIA, derived state, storage. |
| `requestUpdate()` / `update()` | `requestUpdate()` for render-owning hosts; `update()` on any host | Schedule the update cycle, or run it now. Most components should let reactive JSX bindings schedule this automatically. |
| `disconnectedCallback()` | When the browser disconnects the element | Tear down resources owned by the connection and call `super.disconnectedCallback()`. |
| `registerCleanupCallback()` | Registered during setup; invoked during disconnect | Register cleanup for decorator or host integrations that use the lower-level reactive-host API. |
| `adoptedCallback()` | When the browser moves an element to another document | An advanced native custom-element hook; Radiant does not add behavior here. |

## First connection and attribute catch-up

The browser or JSX can set attributes before a custom element has connected. Radiant intentionally defers the first property synchronization until its connection microtask:

1. The browser calls `connectedCallback()`.
2. Radiant runs registered connection callbacks immediately.
3. Radiant queues the connection work and catches up authored attributes. A property assigned before that sync wins over the authored attribute.
4. If `render()` is overridden, Radiant hydrates or renders the initial view; otherwise it keeps the authored light DOM.
5. Radiant flushes `@bindTo` paint, calls `onConnected()`, then `updated(changed)`.

This ordering prevents a constructor default from overwriting an authored attribute and gives `onConnected()` a stable point for post-sync work. If the host is removed before the microtask runs, Radiant aborts the pending work and does not call `onConnected()`.

## Choosing the right hook

Use the smallest lifecycle surface that matches the job:

- Keep authored light DOM in place and copy fields with [@bindTo](/docs/decorators/bind-to). Do not query a node only to copy a reactive value into it.
- Use `@onUpdated('property')` when a procedure must follow property changes (focus, joined ARIA, derived state).
- Render your own UI with JSX bindings when the host owns `render()`.
- Use `onConnected()` when setup needs the final initial property values or the initial rendered view, and repeat that setup after reconnect.
- Use `@ref(...)` or `@query(...)` only when that setup needs a live element handle—for example, to focus an element, observe it, or pass it to an imperative browser API.
- Put connection-scoped teardown in `disconnectedCallback()` and always call `super.disconnectedCallback()`.

## Related documentation

- [RadiantElement](/docs/components/radiant-element) for host modes and render ownership
- [@bindTo](/docs/decorators/bind-to) for copying fields onto existing DOM
- [RadiantController](/docs/components/radiant-controller) for attaching the same reactive model to existing HTML
- [Slots](/docs/components/slots) for where a render-owning host places its children
- [Hydration](/docs/ssr/hydration) for server-rendered host behavior
