liveBook · Manning

Unmounting

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

A component method that destroys the rendered view by removing its DOM content and associated subscriptions.

Unmounting removes a component or application from the DOM and releases the resources that keep it active. It is the cleanup counterpart to mounting: destroy the rendered view, clear stored virtual DOM references, and unsubscribe active listeners. section 9.1.2 section 5.2.5

A reliable unmount operation performs these responsibilities:

  • Call destroyDOM() with the saved virtual DOM so the rendered view and its DOM content are removed. section 9.2
  • Unsubscribe reducers, render handlers, or other component subscriptions so detached code no longer responds to dispatched changes. section 5.2.5 section 9.1.2
  • Clear references such as vdom and host elements, then mark the instance as unmounted. section 9.2 section 12.4
  • Reject invalid lifecycle calls, including unmounting an instance that is not mounted. section 9.2 section 12.4

Component cleanup

A minimal component unmount method is: unmount() { if (!this.#isMounted) throw new Error('Component is not mounted'); destroyDOM(this.#vdom); this.#vdom = null; this.#hostEl = null; this.#isMounted = false }. The same pattern scales to an application by unsubscribing its stored subscriptions after destroying the view. section 9.2 section 5.2.5

Listing 9.1 Basic component methods (component.js)
import { destroyDOM } from './destroy-dom'
import { mountDOM } from './mount-dom'

export function defineComponent({ render }) {  #1
  class Component {  #2
    #vdom = null
    #hostEl = null

    render() {  #3
      return render()
    }

    mount(hostEl, index = null) {
      this.#vdom = this.render()  #4
      mountDOM(this.#vdom, hostEl, index)  #5

      this.#hostEl = hostEl
    }

    unmount() {
      destroyDOM(this.#vdom)  #6

      this.#vdom = null
      this.#hostEl = null
    }
  }

  return Component  #7
}

Inspect the complete component implementation, including the saved virtual DOM, host element, destroyDOM() call, and cleanup assignments. section 9.2

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

How deep it goes
Implements cleanup with destroyDOM(), reference resets, subscription removal, and guards against invalid lifecycle calls.
Examples
Uses JavaScript component and application methods that destroy virtual DOM views, reset state, and unsubscribe dispatcher handlers.
What it assumes
Assumes familiarity with mounting, virtual DOM values, destroyDOM(), and subscriptions while demonstrating cleanup incrementally.
What you can do after
Operate component and application lifecycles safely by removing views, cleaning subscriptions, resetting references, and detecting invalid calls.
How it connects
Connects mounting, the virtual DOM, DOM destruction, component lifecycle, application lifecycle, and subscriptions.

Also explained in

Compare how 2 books explain unmounting →

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.