liveBook · Manning

Component hierarchy

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

A tree-shaped arrangement in which user-interface components contain and render descendant components.

A component hierarchy breaks a large interface into smaller components, each responsible for part of the view and its user interaction. Components compose as a tree: a root component returns children, and those children return further virtual DOM nodes. section 3.8 section 3.8.3

Build the hierarchy by assigning focused responsibilities:

  • Make the application component the root and group multiple returned children in a fragment when one virtual DOM root is required. section 3.8.3
  • Decompose a view into named child components, such as CreateTodo and TodoList, instead of keeping the entire interface in one function. section 3.8
  • Continue downward when a child has its own structure: TodoList renders TodoItem components, which select read or edit subcomponents. section 3.8.3
  • Keep each component as a pure function that receives relevant data and generates its portion of the virtual DOM. section 3.8.3

TODO application tree

App is the root. It renders an h1 element, CreateTodo, and TodoList inside a fragment. TodoList renders a ul containing TodoItem components; each TodoItem renders either TodoInReadMode or TodoInEditMode. The resulting component tree mirrors the application view and the virtual DOM nodes each component produces.

Figure 3.9 The hierarchy of components of the TODOs application, in which each component has its child virtual DOM nodes below itself
figure

Browse the tree to see the root component, nested component functions, fragments, and the virtual DOM nodes generated beneath them. section 3.8.3

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

How deep it goes
Descends from definition to JavaScript decomposition, tree structure, fragments, and conditional child rendering.
Examples
Uses a TODO application with App, CreateTodo, TodoList, TodoItem, and read/edit mode subcomponents.
What it assumes
Assumes basic JavaScript functions and virtual DOM concepts while teaching component decomposition through a small application.
What you can do after
Build it from scratch by decomposing a view into pure component functions and composing their virtual DOM results.
How it connects
Connects component hierarchy with virtual DOM, pure functions, fragments, stateful components, and reducers.

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.