Skip to content

[reactive-element] Initial changed properties can be inconsistent #4916

Closed

Description

@sorvell

Which package(s) are affected?

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

Description

Reactive properties defined via the static properties block v. the property decorator can have inconsistent initial changed values.

This is true for example, for properties defined via accessor or via custom get/set.

Reproduction

https://lit.dev/playground/#gist=9ea51856fe6b624f8dc1d37b62692dd4

See console output. All the of *j and *t properties should match.

Workaround

Hack fix:

static createProperty(name, options) {
  if (this.prototype.hasOwnProperty(name)) {
    options = {...options, wrapped: true};
  }
  super.createProperty(name, options);
}
  
_$changeProperty(name, oldValue, options) {
  oldValue = this.hasUpdated ? oldValue : undefined;
  super._$changeProperty(name, oldValue, options);
}

Is this a regression?

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

Affected versions

3.2.1

Browser/OS/Node environment

All

Activity

  1. self-assigned this
    on Feb 4, 2025
  2. sorvell commented on Feb 4, 2025

    @sorvell
    MemberAuthor

    What should be recorded as an initial change and what old value is reported is not completely clear. Based on the number of ways properties can be defined, this is very hard to normalize.

    These permutations mean there ~18 ways to configure a property.

    • field, accessor, get/set
    • initial value, defaultValue, no value
    • static properties v. decorator

    Conceptually, the changedProperties map represents the changes since the last update.

    When their has been no previous update, I think it makes sense to consider any defined value a change and the "old value" as undefined.

  3. sorvell commented on Mar 20, 2025

    @sorvell
    MemberAuthor

    #4934 (comment) is a concrete example of this causing a problem.

  4. freshp86 commented on Mar 20, 2025

    @freshp86

    #4934 (comment) is a concrete example of this causing a problem.

    Author of the above comment and minimal repro here: This is indeed affecting Chromium code. As part of transitioning off of TypeScript's useDefineForClassFields: false by deploying the accessor keyword, we are unfortunately finding out that we need to add workarounds to make wrapped and non-wrapped properties behave 100% the same (see candidate latest workaround for the inconsistency discussed here). We are also reaching the limits of what can be reasonably tweaked from a LitElement subclass (all of our Lit elements inherit from a CrLitElement class) where we've already been accessing some internal ReactiveElement APIs, which seems fragile.

    When their has been no previous update, I think it makes sense to consider any defined value a change and the "old value" as undefined.

    Agreed. We have always thought of this as as part of the pubilc API behavior of Lit, where any Lit reactive property (regardless of how it is defined, initialized, configured) is reported as transitioning from undefined to some value the first time that is reported as a changed property in willUpdate(), updated(). Perhaps this behavior could be clarified a bit more in https://lit.dev/docs/components/properties/#when-properties-change ?

    Ultimately I think client code shouldn't have to deal with either of implementation details (how was the property defined), or initialization details (was the initial value passed from the parent, or was it initialized to the default value provided by the element that owns the property etc) and ideally all reasonable possible configurations should be covered by automated tests (like the ones we are adding at https://chromium-review.googlesource.com/c/chromium/src/+/6373768/4/chrome/test/data/webui/cr_elements/cr_lit_element_test.ts to test our candidate workarounds). Would happily lend some cycles to update or add any missing tests if we can reach a resolution that this is indeed a bug worth fixing.

    Thanks!

  5. justinfagnani commented on Mar 20, 2025

    @justinfagnani
    Collaborator

    When their has been no previous update, I think it makes sense to consider any defined value a change and the "old value" as undefined.

    This sounds right, with the exception that I don't think we should include the initial default value. In all initial value cases, you could argue that the value is not changing from undefined initially, but we can't really handle that case well with experimental decorators. But with useDefault we can assume that there's an initial value with all three ways to declare a property, so we can treat that as never having transitioned from undefined. And since it's a new opt-in feature, it's backwards compatible.

  6. freshp86 commented on Mar 20, 2025

    @freshp86

    But with useDefault we can assume that there's an initial value with all three ways to declare a property, so we can treat that as never having transitioned from undefined. And since it's a new opt-in feature, it's backwards compatible.

    I don't have much of an opinion of whether useDefault should behave differently, and we don't use useDefault in Chromium. As long as all other cases behave the same, it would be sufficient for us.

    Not reporting useDefault: true properties though as changed from undefined to the initial value could result in a somewhat odd case where willUpdate() and updated() would be triggered even though changed properties would be empty the first time (imagine all Lit properties specify useDefault: true). Why did the element render then if seemingly nothing changed? If changed properties is supposed to hold the "diff" between the previous render cycle and the current one, then I don't think useDefault: true/false should affect it. I guess it depends on how the semantics of "what do changed properties actually mean?" are defined.

  7. justinfagnani commented on Mar 20, 2025

    @justinfagnani
    Collaborator

    Elements can already render with no property changes, from any plain call to requestUpdate(), so there's no problem there.

  8. added a commit that references this issue on Mar 21, 2025
    159ff4d
  9. added a commit that references this issue on Apr 2, 2025
    3e2f87f
  10. added a commit that references this issue on May 7, 2026
    f8dccdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions