Skip to content

Ref directive does not san-check its stored arg on disconnected/reconnected lifecycle #5217

@louis-bompart

Description

@louis-bompart

Which package(s) are affected?

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

Description

❓ is the ref directive supposed to accept an undefined for argument?
Looks like that yes based on the exported types (that are derived from here
However, both the disconnected & reconnected lifecyles are doing unsafe usage of the ref.

I'll be glad to bring a fix upstream for that 🙂
However, I see two possible fixes, and I'd like to see which one we want to go with:
Either we consider the issue to be the looseness of the argument, and restrict it from RefCallback|undefined to RefCallback
Or, we go the opposite way and we ensure the internal _ref property is used safely.****

Reproduction

https://lit.dev/playground/#sample=examples%2Fhello-world&gist=0b002ac6df87f09ff2344481e719cce4

  1. Open DevTools
  2. Check the console
  3. It throw a runtime error

Workaround

Marshall value prior to using the directive, <div ${arg ? ref(arg) : nothing}></div>

Is this a regression?

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

Affected versions

3.3.1

Browser/OS/Node environment

Browser: Chrome 144.0.7559.97
OS: macOS 26.2 (25C56)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    Status
    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions