Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 38 ยท Reactive Properties

The Lit Update Lifecycle Explained

Follow a Lit update from property change to DOM, and learn when to use willUpdate, shouldUpdate, firstUpdated, updated, requestUpdate and updateComplete.

Every Lit component moves through the same sequence each time reactive data changes. Knowing the order of that sequence tells you where each kind of code belongs: computing derived values, measuring the DOM, focusing an input, starting a fetch. Put the code in the wrong hook and you get double renders, stale measurements or infinite loops.

This lesson walks through the update cycle, then through each hook with a runnable example that logs the order for you.

Two lifecycles in one

A Lit element has the standard custom element callbacks from the browser (constructor, connectedCallback, disconnectedCallback, attributeChangedCallback) and the reactive update cycle that Lit adds on top.

Browser callbacks

constructor Set defaults
connectedCallback Added to the page
disconnectedCallback Removed; clean up

Call super.connectedCallback() when you override.

1 / 2

The full order for one update is:

  1. A property changes, so requestUpdate() queues a microtask.
  2. shouldUpdate(changedProperties): return false to skip the update.
  3. willUpdate(changedProperties): compute values; no extra render is triggered by changes made here.
  4. update() calls render() and writes the result to the DOM.
  5. firstUpdated(changedProperties): only after the very first render.
  6. updated(changedProperties): after every render.
  7. The updateComplete promise resolves.
Watch the order

Read the console from top to bottom. Notice that firstUpdated appears only in the first cycle, and that changedProperties is a Map of property name to previous value.

willUpdate: compute derived values

willUpdate runs before render(). Changing a property inside it does not cause another update, because the update is already in progress. That makes it the right home for values derived from other properties.

Derived values in willUpdate

For a cheap calculation you can skip state entirely and compute inside render(). Use willUpdate when the work is expensive or when several parts of the template need the same value.

firstUpdated and updated: the DOM exists

Anything that needs real DOM nodes (focus, measuring, third-party widgets) belongs in firstUpdated or updated. They run after Lit has written to the shadow root, so this.renderRoot.querySelector(...) finds your elements.

Focus after first render

Changing a reactive property inside updated() schedules another update, and Lit logs a dev-mode warning. That is sometimes needed (for example after measuring the DOM), but guard it with changed.has(...) or you will create an endless loop. If you can compute the value earlier, use willUpdate.

updateComplete and requestUpdate

Every element has an updateComplete promise that resolves when the current pending update has finished (and no newer one was queued during it). Await it before reading the DOM, and in tests.

JavaScript
el.count = 10;
console.log(el.shadowRoot.textContent); // still the OLD text
await el.updateComplete;
console.log(el.shadowRoot.textContent); // new text

requestUpdate() queues an update manually. It is the tool for data Lit cannot see: mutated arrays, a Map, or a value outside the element.

requestUpdate for mutations

items is deliberately a plain field here, so assigning a new array would not render by itself; requestUpdate() is the explicit trigger.

shouldUpdate and the changedProperties map

shouldUpdate(changedProperties) lets you skip an update entirely, for example while the element is hidden or still waiting for data. If you return false, willUpdate, render and updated do not run, and the changes stay recorded until the next update that goes through.

JavaScript
shouldUpdate(changed) {
  return this.data !== undefined;   // wait for data before first render
}

The changedProperties map is also how you react to specific changes:

JavaScript
updated(changed) {
  if (changed.has("userId")) {
    this._load(this.userId);        // start a fetch only when the id changed
  }
}

Cleaning up in disconnectedCallback

Elements can be removed and re-added. Anything you start in connectedCallback (global listeners, timers, observers) must be stopped in disconnectedCallback, or it leaks. Always call super first in both.

JavaScript
connectedCallback() {
  super.connectedCallback();
  window.addEventListener("resize", this._onResize);
}
disconnectedCallback() {
  window.removeEventListener("resize", this._onResize);
  super.disconnectedCallback();
}

Define _onResize as an arrow-function field (or bind it in the constructor) so the same function reference is used for add and remove. Reactive controllers package this connect/disconnect pairing into reusable pieces.

Choosing the right hook

You want to... Use
Set defaults constructor
Add global listeners connectedCallback
Derive a value from properties willUpdate
Skip rendering shouldUpdate
Focus or measure DOM, init a widget once firstUpdated
React to specific property changes after render updated
Wait for the DOM in tests or scripts await el.updateComplete
Force a render after mutation requestUpdate()

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where should you compute a value derived from two properties without causing an extra render?

    Changes made in willUpdate happen before render in the same cycle, so they do not queue another update.
  2. 2. What is the correct way to read the new DOM right after setting a property?

    Updates are asynchronous; updateComplete resolves once the pending update has been written to the DOM.
  3. 3. Which hook runs only once, after the first render?

    firstUpdated runs after the first render only; updated runs after every render.
  4. 4. Why is it risky to set a reactive property inside updated() without a guard?

    A property change schedules a new update, whose updated() changes it again. Guard with changed.has(...).
  5. 5. When must you call requestUpdate() yourself?

    Lit only watches reactive property assignments; in-place mutation is invisible to it.

Summary

  • A change queues an asynchronous update: shouldUpdate, willUpdate, render, firstUpdated (once), updated.
  • Compute derived values in willUpdate; touch the DOM in firstUpdated/updated.
  • await el.updateComplete before reading the DOM; use requestUpdate() for invisible changes.
  • Pair every connectedCallback side effect with a disconnectedCallback cleanup.

Next: Reactive Controllers