liveBook · Manning

View

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

The rendered interface that represents an application's current state to the user.

A view is the visible interface an application creates from its current state. Rendering turns data into something a user can see: a to-do stored as a string, for example, becomes HTML containing its description and controls. A view is not necessarily fixed when the page loads; JavaScript can create it and change it as the application runs. section 2.2.3 section 1.3.2

It helps to separate three responsibilities:

  • State holds the data, such as an array of to-dos or a counter value. section 2.2.3 section 6.2
  • A view description says what should appear for that state. A virtual DOM can serve as this description. section 3.0 section 3.1
  • Rendering creates the visible HTML. The Document API lets JavaScript create elements, set their content, and attach them to the document. section 1.3.2 section 2.2.3

In a single-page application, the browser first loads a mostly empty HTML page along with JavaScript and CSS. JavaScript then creates the initial view, a step called mounting. When navigation changes the URL, a router can update the view without reloading the entire application. section 1.3.2

A to-do with two views

Start with a string in a to-do array. In read mode, render it as a list item containing its text and a Done button. On a double-click, replace that list item with an edit-mode item containing an input and Save and Cancel buttons. Saving updates the array and replaces the item with a new read-mode rendering; canceling restores the read-mode rendering. The underlying to-do and its visible representation have different jobs. section 2.2.3

Direct DOM manipulation works, but mixing element creation and updates with application logic makes the code harder to maintain. A framework can instead accept a description of the desired view and handle the Document API work. In a small counter example, a view function receives the count and an event emitter, describes a button displaying that count, and emits an action when clicked. This simple framework replaces the button element after each state change; the user sees its number change. section 3.0 section 3.1 section 6.2

A component can own a smaller part of the view. A stateful component has its own state and lifecycle and updates its view when that state changes; an instance can manage its own DOM subtree. The same state-to-view idea applies whether the visible result is one button or part of a larger interface. section 9.1 section 9.4

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

How deep it goes
Works through manual element creation and replacement, then shows a framework-managed counter; state management is treated separately.
Examples
Uses read- and edit-mode to-dos and a button whose displayed count changes after clicks.
What it assumes
Introduces rendering explicitly while using basic JavaScript, HTML elements, event listeners, and browser DOM operations.
What you can do after
Build it from scratch with JavaScript DOM operations, then describe it for a small framework to render.
How it connects
Connects state, rendering, the Document API, the virtual DOM, routing, and stateful components.

Also explained in

Compare how 4 books explain view →

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.