This page is part of a static HTML representation of the TiddlyWiki at https://tiddlywiki.com/dev/

Widget `destroy` method examples

1st June 2023 at 12:50pm

When using a v-dom library

Virtual DOM libraries manages its internal state and apply state to DOM periodically, this is so called "controlled" component. When Tiddlywiki remove a DOM element controlled by a v-dom library, it may throws error.

So when creating a plugin providing v-dom library binding, you need to tell v-dom library (for example, React.js) the DOM element is removed. We will use destroy method for this.

  render() {
    // ...other render related code
    if (this.root === undefined || this.containerElement === undefined) {
      // initialize the v-dom library
      this.root = ReactDom.createRoot(document.createElement('div'));
    }
  }

  destroy() {
    // end the lifecycle of v-dom library
    this.root && this.root.unmount();
  }

The destroy method will be called by parent widget. If you widget don't have any child widget, you can just write your own tear down logic. If it may have some child widget, don't forget to call original destroy method in the Widget class to destroy children widgets.

Widget.prototype.destroy();
this.root && this.root.unmount();
/** if you are using ESNext
super.destroy();
this.root?.unmount();
*/