question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling requestUpdate() from attributeChangedCallback() doesn't update.

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sorvellcommented, Mar 11, 2021

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 that requestUpdate does not queue an additional update.

In this case, I would suggest not putting your code into attributeChangedCallback but instead putting it in render or update or updated.

update(changedProperties) {
  if (changedProperties.has(propertyWithNewValue)) {
    this.myClassMap = {
      ...this.myClassMap,
      foo: newValueBasedOnChangedProperty,
    };
  }
}

If you also have an attribute that is not tied to a reactive property, you can manually call this.requestUpdate() in attributeChangedCallback 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.

0reactions
Halt001commented, Mar 12, 2021

I can also confirm that your suggested workaround works in our Angular app. Thank you for your help.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found