Skip to content

Root

reference
1 min readUpdated

Kind: Interface

Source: src/jsx/dom/client.ts

Part of: Jsx

Root represents a mounted JSX DOM tree created by the client renderer. It exposes methods to render the tree and to remove the mounted tree from its container.

Diagram

mermaid
graph LR
  App[JSX application] --> Root[Root]
  Root --> Render[render()]
  Root --> Unmount[unmount()]
  Render --> DOM[DOM container]
  Unmount --> DOM

Usage

ts
import { createRoot } from "./jsx/dom/client";

const container = document.getElementById("app");

if (!container) {
  throw new Error("Missing app container");
}

const root = createRoot(container);

root.render();

window.addEventListener("beforeunload", () => {
  root.unmount();
});

AI Coding Instructions

  • Call render() after creating a Root to mount or update its JSX DOM tree.
  • Call unmount() when the owning application or view is removed.
  • Keep the Root instance associated with the DOM container it was created for.
  • Do not call render() after unmount() unless the client renderer supports remounting.

How it works

Root is the interface for the object returned by createRoot. It has two void-returning lifecycle methods: render(children: Child) and unmount() (src/jsx/dom/client.ts:11-14). Child includes strings, numbers, JSX nodes, null, undefined, booleans, promises of strings, and nested child arrays (src/jsx/base.ts:162-170).

Relationships

  • IMPORTS → useState
  • IMPORTS → buildNode
  • IMPORTS → renderNode

Was this page helpful?

Download as PDF
Root — Hono (narrator proof)