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
According to the documentation if an observed property's hasChanged() returns true, LitElement starts an element update; returning false will result in no updates. But this isn't always the case, if you have multiple properties with multiple hasChanged methods, and some return true, others return false. LitElement will still update properties whose hasChanged() return false.
`
import {LitElement, html} from '@polymer/lit-element';
class MyElement extends LitElement {
static get properties() {
return {
As long as there's one property's hasChanged() returns true, everything gets updated
That is the expected behavior - the return value is used to say whether the property changing should trigger an update. As long as at least one property says "yes, update" then an update happens.
(hasChanged is a confusing name IMO because it is easy to think it's referring to whether the property has changed but it only get's called if it has)
That is the expected behavior - the return value is used to say whether the property changing should trigger an update. As long as at least one property says "yes, update" then an update happens.
I see what you mean, but IMO this is very confusing; the current behavior allows prop1 to control whether prop2 gets updated regardless of what prop2's hasChanged returns (true or false).
We can try to clarify the documentation here. Here's the way we think about it. First, a property does not, itself, update, the element does. The element updates and calls its render method when a registered property changes or requestUpdate is called.
A property changes whenever it's set to a different value. For properties of non-trivial type (i.e. objects), sometimes you want more direct control over whether or not the update system sees a "set" as a change. In particular, maybe an object's key updates and this should indicate that it's different and an update should occur. This is the intended usage for which hasChanged was designed.
If you want more direct control over the setting of a property, you can implement a getter/setter and call requestUpdate(name, oldValue) in the setter when an update should occur.
If you want to take some specific custom action (that you cannot achieve in the render method) based on the set of properties that have changed, you can implement updated(changedProperties) and take action based on the entire state of the element at that time.
@sorvell Thanks for the clarification. I think updating the doc to include how hasChanged() should be used and maybe some examples will definitely help other users.
ghost changed the title [-]Property hasChanged() may not be called during updates[/-][+][docs] Clarify docs on hasChanged & update lifecycle (e.g. multiple property changes)[/+]on Dec 13, 2018
ghost added a commit that references this issue on Dec 18, 2018
Activity
CaptainCodeman commentedon Nov 28, 2018
That is the expected behavior - the return value is used to say whether the property changing should trigger an update. As long as at least one property says "yes, update" then an update happens.
(hasChanged is a confusing name IMO because it is easy to think it's referring to whether the property has changed but it only get's called if it has)
jason-gu commentedon Nov 28, 2018
I see what you mean, but IMO this is very confusing; the current behavior allows prop1 to control whether prop2 gets updated regardless of what prop2's hasChanged returns (true or false).
sorvell commentedon Dec 12, 2018
We can try to clarify the documentation here. Here's the way we think about it. First, a property does not, itself, update, the element does. The element updates and calls its
rendermethod when a registered property changes orrequestUpdateis called.A property changes whenever it's set to a different value. For properties of non-trivial type (i.e. objects), sometimes you want more direct control over whether or not the update system sees a "set" as a change. In particular, maybe an object's key updates and this should indicate that it's different and an update should occur. This is the intended usage for which
hasChangedwas designed.If you want more direct control over the setting of a property, you can implement a getter/setter and call
requestUpdate(name, oldValue)in the setter when an update should occur.If you want to take some specific custom action (that you cannot achieve in the
rendermethod) based on the set of properties that have changed, you can implementupdated(changedProperties)and take action based on the entire state of the element at that time.jason-gu commentedon Dec 12, 2018
@sorvell Thanks for the clarification. I think updating the doc to include how hasChanged() should be used and maybe some examples will definitely help other users.
[-]Property hasChanged() may not be called during updates[/-][+][docs] Clarify docs on hasChanged & update lifecycle (e.g. multiple property changes)[/+]Fixes #318