liveBook · Manning

Root component

As explained in Build a Frontend Web Framework (From Scratch)

The topmost component in a component hierarchy from which the application's interface is rendered.

A root component sits at the top of an application's component hierarchy and contains the whole application view, divided into subcomponents. Passing it to createApp() gives the framework a single starting point for rendering the interface. section 9.0 section 12.4

The root component establishes several practical responsibilities:

  • Provide the view from which the component hierarchy is rendered. section 9.0
  • Receive optional props through createApp(RootComponent, props). section 12.4
  • Let child components own their state instead of requiring one global state object and reducers. section 9.0 section 12.4
  • Serve as the reference point for DOM patching without a parent component or component offset. section 9.3.4

Mounting a root component

function createApp(RootComponent, props = {}) { let vdom = null return { mount(parentEl) { vdom = h(RootComponent, props) mountDOM(vdom, parentEl) }, unmount() { destroyDOM(vdom) vdom = null }, } } The application creates a virtual DOM representation of the root component, mounts it into the parent element, and later destroys it during unmounting. Production-style framework code also guards against mounting an already-mounted application or unmounting one that is not mounted. section 12.4

Figure 12.7 The new version of the framework’s application instance requires only the root component.
figure

Compare the earlier application shape, which requires global state and reducers, with the newer shape, which requires only the root component. section 12.4

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Descends from hierarchy role to createApp wiring, mounting, unmounting, and fragment offset handling.
Examples
Uses createApp(), h(), mountDOM(), destroyDOM(), and diagrams contrasting global-state and root-component application instances.
What it assumes
Assumes familiarity with components and virtual DOM operations while simplifying state management through component-owned state.
What you can do after
Build an application entry point that mounts and unmounts a root component with optional props.
How it connects
Connects component hierarchy, component state, props, virtual DOM, reducers, and DOM patching.

Also explained in

Compare how 2 books explain root component →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.