Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
Convert prototype to ES6 Class

Trying to convert a js "class" written using prototypes into an ES6 class, the new version runs without errors but does not display any content in the DOM whereas the old version does.

The specifics of how the class works shouldn't be important, however, I'm trying to implement a custom web component in preact.

// common imports for completeness
import { h, render, FunctionComponent, VNode } from "preact";
// old version: working ✅

function connectedCallback() {
  this._vdom = toVdom(this, this._vdomComponent);
  render(this._vdom, this._root);
}

function disconnectedCallback() {
  render((this._vdom = null), this._root);
}

function toVdom(element: HTMLElement, nodeName?: FunctionComponent<any>) {
  if (element.nodeType === 3) return element.data;
  if (element.nodeType !== 1) return null;
  const children = Array.from(element.childNodes).reverse().map(cn => toVdom(cn));
  return h(nodeName || element.nodeName.toLowerCase(), {}, children);
}

function createCustomElement(Component: FunctionComponent<any>) {
  function PreactElement() {
    const inst = Reflect.construct(HTMLElement, [], PreactElement);
    inst._vdomComponent = Component;
    inst._root = document.body;
    return inst;
  }
  PreactElement.prototype = Object.create(HTMLElement.prototype);
  PreactElement.prototype.constructor = PreactElement;
  PreactElement.prototype.connectedCallback = connectedCallback;
  PreactElement.prototype.detachedCallback = disconnectedCallback;

  return PreactElement;
}
// new version: not working ❌

const createCustomElement = (Component: FunctionComponent<any>) => {
  return class PreactElement extends HTMLElement {
    private readonly _vdomComponent = Component;
    private readonly _root = document.body;
    private _vdom: VNode<any> | null = null;

    private static toVdom = (element: HTMLElement, nodeName?: FunctionComponent<any>) => {
      if (element.nodeType === 3) return element.data;
      if (element.nodeType !== 1) return null;
      const children = Array.from(element.childNodes).reverse().map(cn => PreactElement.toVdom(cn));
      return h(nodeName || element.nodeName.toLowerCase(), {}, children);
    }

    connectedCallback = () => {
      this._vdom = PreactElement.toVdom(this, this._vdomComponent);
      render(this._vdom, this._root);
    }

    disconnectedCallback = () => {
      render((this._vdom = null), this._root);
    }
  };
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const createCustomElement = (Component: FunctionComponent<any>) => {
  return class PreactElement extends HTMLElement {
    private readonly _vdomComponent = Component;
    private readonly _root = document.body;
    private _vdom: VNode<any> | null = null;

    private static toVdom = (element: HTMLElement, nodeName?: FunctionComponent<any>) => {
      if (element.nodeType === 3) return element.data;
      if (element.nodeType !== 1) return null;
      const children = Array.from(element.childNodes).reverse().map(cn => PreactElement.toVdom(cn));
      return h(nodeName || element.nodeName.toLowerCase(), {}, children);
    }

    connectedCallback() {
      this._vdom = PreactElement.toVdom(this, this._vdomComponent);
      render(this._vdom, this._root);
    }

    disconnectedCallback() {
      render((this._vdom = null), this._root);
    }
  };
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!