Skip to content

ReactiveElement: calling performUpdate before an element is in the DOM causes exception #4268

@sorvell

Description

@sorvell

Which package(s) are affected?

Lit Core (lit / lit-html / lit-element / reactive-element)

Description

When an element is imperatively created and performUpdate() is called on it before adding it to the DOM, an exception is generated and the element does not render when it is added to the DOM.

This error occurs because the element's renderRoot is created in connectedCallback. This issue could be fixed by creating the render root on the first update.

Reproduction

https://lit.dev/playground/#gist=d5f782efaf4758e2d17f68a77216c38c

const e = document.createElement('my-element');
e.performUpdate();
// Uncaught TypeError: The container to render into may not be undefined

Workaround

performUpdate() {
  if (this.renderRoot === undefined) {
    this.renderRoot = this.createRenderRoot();
  }
  super.performUpdate();
}

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

2.x, 3.x

Browser/OS/Node environment

All

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions