Foreign renders a container element whose children are owned by external (non-Vertz) code. During hydration, the container is claimed but its children are not walked — external content is preserved.
Use Foreign to integrate third-party libraries that manage their own DOM (D3, Chart.js, CodeMirror, map libraries, etc.).
Signature
Props
| Prop | Type | Default | Description |
|---|---|---|---|
tag | keyof HTMLElementTagNameMap | keyof SVGElementTagNameMap | 'div' | HTML/SVG tag for the container element |
onReady | (container: HTMLElement | SVGElement) => (() => void) | void | — | Called when the container is mounted and ready for external DOM manipulation. Return a cleanup function for unmount. |
id | string | — | Element id attribute |
className | string | — | CSS class name |
style | Partial<CSSStyleDeclaration> | — | Inline styles (camelCase object) |
children | never | — | Not supported. External code manages children via onReady. |
Usage
Wrapping a chart library
Wrapping a rich text editor
With reactive data via domEffect
Bridge Vertz reactivity into the unmanaged subtree usingdomEffect:
Hydration behavior
During server-side rendering,Foreign renders as an empty container element (e.g., <div></div>). On hydration:
- The container element is claimed by the hydration cursor
- Children are not walked — the cursor advances past the container
onReadyfires after hydration completes (post-hydrationonMounttiming)- Any server-rendered children inside the container are preserved for the
onReadycallback to use or replace
Cleanup
The function returned fromonReady runs when the component unmounts (e.g., route change, conditional removal). Use it to tear down third-party library instances, remove event listeners, or cancel animations.