Skip to content

fix(lit-html): handle undefined arg on disconnect for ref directive #5218

Merged
justinfagnani merged 3 commits into
lit:mainfrom
louis-bompart:fix/5217
Mar 12, 2026
Merged

fix(lit-html): handle undefined arg on disconnect for ref directive #5218
justinfagnani merged 3 commits into
lit:mainfrom
louis-bompart:fix/5217

Conversation

@louis-bompart

Copy link
Copy Markdown
Contributor

When ref is called with undefined for arg and the element where the ref has been render in disconnect, the disconnect method tried to call this._ref.value, where this._ref being undefined, causing an error.

The _updateRefValue method in essence currently already sieve thru the various type of the _ref member. This PR handles the undefined case by returning early.

Fixes #5217

@changeset-bot

changeset-bot Bot commented Jan 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c972037

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
lit Patch
lit-html Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

override update(part: ElementPart, [ref]: Parameters<this['render']>) {
const refChanged = ref !== this._ref;
if (refChanged && this._ref !== undefined) {
if (refChanged) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe an over-optimization, but we would have been checking twice whether this._ref is undefined in this code path.
Given that _updateRefValue handles it after my change, I removed this part of the condition.

@sorvell sorvell requested a review from justinfagnani February 4, 2026 01:47
Comment thread .changeset/sweet-garlics-behave.md
@justinfagnani justinfagnani merged commit be6b275 into lit:main Mar 12, 2026
8 checks passed
kyubisation pushed a commit to kyubisation/lit that referenced this pull request May 7, 2026
@lit-robot lit-robot mentioned this pull request May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants