Calling requestUpdate() from attributeChangedCallback() doesn't update.
See original GitHub issueWhen I call this.requestUpdate() from within attributeChangedCallback(…), because I changed a classMap, the render function is not called.
When I call this.requestUpdate()
within a timeout it does seem to work. Is this the way to do it or is this a bug?
attributeChangedCallback(name: string, oldVal: AttributeType, newVal: AttributeType) {
super.attributeChangedCallback(name, oldVal, newVal);
...
this.myClassMap = {
...this.myClassMap,
foo: newValueBasedOnChangedProperty,
}
// this doesn't seem to do anything
this.requestUpdate();
// this does trigger a re-render
setTimeout(() => this.requestUpdate(), 0);
}
What also seems to work is waiting for the updateComplete promise using this:
this.updateComplete.then(
() => this.requestUpdate()
);
But it still feels like I’m putting the cart before the horse.
Versions
- lit-element: v2.4.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
LitElement: Calling requestUpdate() from ... - Stack Overflow
... Calling requestUpdate() from attributeChangedCallback() doesn't update ... after I changed a classMap the render function is not called.
Read more >Secondary property change not reflected when first ... - GitHub
Description When a second property is changed internally due to a change in another property via setting its attribute, the second property ...
Read more >Lifecycle - Lit.dev
Use requestUpdate to trigger an update lifecycle when LitElement cannot pick it up. ... When an update is performed, the performUpdate() method is...
Read more >UpdatingElement | @polymer/lit-element
When properties change, the update method is asynchronously called. ... The Promise result is false if a property was set inside updated() ....
Read more >How to Re render DOM on value change in lit-html
attributeChangedCallback triggers whenever a property changes in static ... requestUpdate() is lit-html life cycle hook which triggers DOM.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
OK, I was able to reproduce this if the attribute is reflected and is changing as a result of the property being set and implemented a fix here: https://stackblitz.com/edit/lit-element-demo-wdpubo?file=src%2Fmy-element.js.
In this case we are setting the attribute during and update and therefore
attributeChangedCallback
is called during the update so thatrequestUpdate
does not queue an additional update.In this case, I would suggest not putting your code into
attributeChangedCallback
but instead putting it inrender
orupdate
orupdated
.If you also have an attribute that is not tied to a reactive property, you can manually call
this.requestUpdate()
inattributeChangedCallback
to ensure that an update is processed.If you need to respond based on rendering having occurred, you can put this code into
updated
instead.Hope that helps.
I can also confirm that your suggested workaround works in our Angular app. Thank you for your help.