Skip to content

[docs] Clarify docs on hasChanged & update lifecycle (e.g. multiple property changes) #318

Closed

Description

@jason-gu

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 {

		myProp: {
			type: String,
			attribute: 'my-prop',
			reflect: true,
			hasChanged(newValue, oldValue) {
				return true;
			}
		},

		someProp: {
			type: String,
			hasChanged(newValue, oldValue) {
				return false;
			}
		}
	};
}

constructor() {
	super();
	this.myProp = 'myProp';
	this.someProp = 'someProp';
}

attributeChangedCallback(name, oldValue, newValue) {
	super.attributeChangedCallback(name, oldValue, newValue);
}

changeAttributes() {
	let randomString = Math.floor(Math.random()*100).toString();
	this.setAttribute('my-prop', 'my-prop ' + randomString);
	this.setAttribute('someprop', 'someprop ' + randomString);
	this.requestUpdate();
}

changeProperties() {
	let randomString = Math.floor(Math.random()*100).toString();
	this.myProp = 'myProp ' + randomString;
	this.someProp = 'someProp ' + randomString;
}

render() {
	return html

${this.myProp}

${this.someProp}

Change Attributes Change Properties ; }
updated() {
	console.log(this);
}

}

customElements.define('my-element', MyElement);
`

Expected Results

Properties whose hashChanged() returning false should not be updated.

Actual Results

As long as there's one property's hasChanged() returns true, everything gets updated.

Browsers Affected

  • [*] Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11

Versions

  • "@polymer/lit-element": "latest",
  • "@webcomponents/webcomponentsjs": "^2.2.0",
  • "lit-html": "latest"

Activity

  1. CaptainCodeman commented on Nov 28, 2018

    @CaptainCodeman

    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)

  2. jason-gu commented on Nov 28, 2018

    @jason-gu
    Author

    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).

  3. sorvell commented on Dec 12, 2018

    @sorvell
    Member

    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.

  4. jason-gu commented on Dec 12, 2018

    @jason-gu
    Author

    @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.

  5. ghost
    self-assigned this
    on Dec 13, 2018
  6. 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
  7. ghost added a commit that references this issue on Dec 18, 2018
    0130853
  8. Repository owner unassigned on Dec 18, 2018
  9. ghost closed this as completedin #380on Dec 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions