Skip to content

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

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)

Activity

  1. sorvell commented on Jan 30, 2026

    @sorvell
    Member

    Thanks for making the issue. I think we need to go with this fix:

    we ensure the internal _ref property is used safely

    It's not super clear to me why supporting undefined as an actual ref argument is important, but it is tested (although obviously not on disconnection) so I think removing support could be called breaking-ish.

    PR welcome. Thanks!

  2. added a commit that references this issue on Jan 30, 2026
    1af86e1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions