UNPKG

873 BJavaScriptView Raw
1/**
2 * Function to unmount a mounted functional component. This deletes the base element of the component from the DOM.
3 * @example
4 *
5 * ```
6 * // First mount component:
7 * let title = mount(<Title message='Hello World!'/>, 'header')
8 * // Sometime later unmount it by passing the vnode to unmount:
9 * unmount(title)
10 ```
11 * @typedef {import('./vnode').VNode} VNode
12 * @param {VNode} vnode The virtual node of the component to unmount.
13 * @return {void} undefined
14 */
15export function unmount(vnode) {
16 /**
17 * Function to remove the base element of a functional component from the DOM.
18 * @return {void} undefined
19 */
20 function doneUnmounting() {
21 vnode.element.parentNode.removeChild(vnode.element)
22 vnode.element = null
23 }
24 if (vnode.props['onunmount']) {
25 vnode.props['onunmount'](doneUnmounting, vnode.element)
26 } else {
27 doneUnmounting()
28 }
29}