mount()

Mount a component tree into a DOM container.

const unmount = mount(element, container)

Parameters

ParameterTypeDescription
element VNodeChild A component element, typically created by JSX. A real DOM node, a string, a number, an array or a thunk is also accepted. For advanced use, can also be created via h().
container Element | string The DOM element to mount into. If a string is provided, it is used as a CSS selector passed to document.querySelector().

Returns

An unmount function that disposes all reactive effects and clears the container.

ReturnTypeDescription
unmount () => void Disposes the root the tree was built in, and with it everything the tree created: its components, their effects (useEffect, onMount and a bare effect() alike), computeds, bindings, list rows and onCleanup callbacks. Then empties the container element.

Usage

Basic Mount

import { mount } from 'what-framework';

function App() {
  return <h1>Hello, World!</h1>;
}

mount(<App />, '#app');

With a DOM Element

const container = document.getElementById('root');
mount(<App />, container);

Unmounting

const unmount = mount(<App />, '#app');

// Later: clean up everything
unmount();

Re-mounting

// mount() automatically cleans up any previous mount
// in the same container before rendering the new tree.
mount(<AppV1 />, '#app');

// This disposes AppV1 and mounts AppV2
mount(<AppV2 />, '#app');

Typical Entry Point

// src/main.jsx: the standard entry point
import { mount } from 'what-framework';
import { App } from './App';

mount(<App />, document.getElementById('app'));

How It Works

  1. Disposes the tree already mounted or hydrated in the container, if there is one
  2. Clears the container's content
  3. Builds the element tree into real DOM nodes inside a new root, the owner of everything the tree creates
  4. Appends the resulting DOM to the container

The returned unmount function reverses this: it disposes that root, which disposes every component, effect, binding and cleanup the tree created, then empties the container. Components are owners, so a bare effect() in a component body goes with its component like everything else.

Notes

  • mount() clears the container before rendering. Existing content is removed.
  • If a previous component tree was mounted or hydrated in the same container, it is disposed before the new tree is created: its components, their effects (a bare effect() in a component body included) and their onCleanup callbacks.
  • The string selector form uses document.querySelector(). Make sure the element exists in the DOM before calling mount(). If the selector matches nothing, mount() throws TypeError: Cannot set properties of null (setting 'textContent') from inside the DOM layer, which points at the framework rather than at your selector.
  • This is the primary entry point for What applications. Every app calls mount() once in its entry file.
  • For a server-rendered page use hydrate(element, container) from what-framework instead. It reuses the server's DOM rather than clearing it. The server half is renderToString / renderToHydratableString / renderToStream from what-framework/server. See the SSR guide.