You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[reactive-element] Initial changed properties can be inconsistent #4916
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.
#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.
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.
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.
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.
Activity
sorvell commentedon Feb 4, 2025
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.
Conceptually, the
changedPropertiesmap 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.sorvell commentedon Mar 20, 2025
#4934 (comment) is a concrete example of this causing a problem.
freshp86 commentedon Mar 20, 2025
Author of the above comment and minimal repro here: This is indeed affecting Chromium code. As part of transitioning off of TypeScript's
useDefineForClassFields: falseby deploying theaccessorkeyword, 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 aLitElementsubclass (all of our Lit elements inherit from aCrLitElementclass) where we've already been accessing some internalReactiveElementAPIs, which seems fragile.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
undefinedto some value the first time that is reported as a changed property inwillUpdate(),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!
justinfagnani commentedon Mar 20, 2025
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
undefinedinitially, but we can't really handle that case well with experimental decorators. But withuseDefaultwe 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 fromundefined. And since it's a new opt-in feature, it's backwards compatible.freshp86 commentedon Mar 20, 2025
I don't have much of an opinion of whether
useDefaultshould behave differently, and we don't useuseDefaultin Chromium. As long as all other cases behave the same, it would be sufficient for us.Not reporting
useDefault: trueproperties though as changed fromundefinedto the initial value could result in a somewhat odd case wherewillUpdate()andupdated()would be triggered even though changed properties would be empty the first time (imagine all Lit properties specifyuseDefault: 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 thinkuseDefault: true/falseshould affect it. I guess it depends on how the semantics of "what do changed properties actually mean?" are defined.justinfagnani commentedon Mar 20, 2025
Elements can already render with no property changes, from any plain call to
requestUpdate(), so there's no problem there.WebUI: CrLitElement wrapped property workaround for inherited values.
Fixes #4916
[reactive-element] fixes inconsistent initial values in changed prope…
[reactive-element] fixes inconsistent initial values in changed prope…