Overview
ReactiveElement is the low-level base class that powers LitElement. It extends HTMLElement and implements the reactive update lifecycle, property/attribute management, and controller integration. Use this package when you want to build your own component base class without lit-html templating.
@lit/reactive-elementExtends:
HTMLElementImplements:
ReactiveControllerHost
Static members
observedAttributes
properties declaration. Calling this getter also triggers finalize() if it has not already run.
properties
PropertyDeclaration options. Declaring a property here creates a reactive accessor that requests an update whenever the property is set.
styles
CSSResult (from the css tag), a CSSStyleSheet, or an array of either.
elementStyles
styles. Created lazily during finalize(). Override finalizeStyles() to customize how styles are processed.
shadowRootOptions
attachShadow() when creating the element’s shadow root. Defaults to {mode: 'open'}.
addInitializer()
ReactiveController.
A function called with the newly constructed element instance.
finalize()
properties block, reads decorator metadata, builds the attribute-to-property map, and resolves elementStyles. Called automatically via observedAttributes. You should not need to call this directly.
Instance properties
updateComplete
true if the update completed without triggering another update, or false if a property was set inside updated(). The promise rejects if an exception is thrown during the update.
To await additional async work after an update, override getUpdateComplete().
hasUpdated
true after the element’s first update completes. You can rely on renderRoot existing once hasUpdated is true.
isUpdatePending
true when an update has been requested but has not yet completed. Read-only in practice.
Instance methods
requestUpdate()
hasChanged option and changedProperties map are handled correctly.
The name of the property that changed.
The previous value of the property.
Property declaration options to use instead of the class-level options.
performUpdate()
shouldUpdate(), willUpdate(), update(), firstUpdated() (on first update), and updated(). Normally runs as a microtask; call directly only when you need to flush an update immediately.
scheduleUpdate()
Promise, the update awaits it before proceeding. Must call super.scheduleUpdate().
shouldUpdate()
update() runs. Returns true by default. Override to conditionally skip updates.
Map of properties that changed, with their previous values.
Return
true to proceed with the update, false to abort.willUpdate()
update(). Use this to compute derived values that are needed during the update. Does not have access to the updated DOM.
Map of properties that changed, with their previous values.
update()
LitElement) to render templates. Setting properties inside update() does not trigger another update.
Map of properties that changed, with their previous values.
firstUpdated()
Map of properties that changed, with their previous values.
updated()
Map of properties that changed, with their previous values.
getUpdateComplete()
updateComplete promise. Override this instead of the getter to await additional async work after an update completes.
Resolves to
true if the update completed without scheduling a further update.addController()
ReactiveController with this element. The controller’s lifecycle callbacks are called in sync with the element’s lifecycle. If the element is already connected, hostConnected() is called immediately.
The controller instance to register.
removeController()
ReactiveController from this element.
The controller instance to remove.
Types
PropertyDeclaration
Options that configure how a reactive property behaves.
When
true, the property is internal state. It is not observed as an attribute and should not be set by users.The attribute name to observe.
false disables attribute observation. Defaults to the lowercased property name.A type hint passed to the
converter to help deserialize the attribute value. Supports String, Number, Boolean, Object, and Array.A function or object with
fromAttribute and toAttribute methods. Converts between attribute strings and property values.When
true, the property value is reflected back to the corresponding attribute after each update.When
true, no reactive accessor is created. You must call requestUpdate() manually when the property changes.When
true, treats the initial property value as a default. The initial value does not reflect to an attribute. When the attribute is removed, the property reverts to this default.Custom change detection function. Return
true to request an update. Defaults to a strict identity check (!Object.is(value, oldValue)).PropertyDeclarations
PropertyDeclaration options. Assigned to static properties.
PropertyValues
PropertyValues<this> in subclass overrides for stronger typing.
AttributeConverter
fromAttribute. The object form supports both directions, which is required when using reflect: true.
Related
ReactiveController
Interface for composable lifecycle integrations.
Reactive properties
How property declarations work in practice.
Lifecycle
Full update lifecycle sequence.
LitElement
The full-featured component base class built on
ReactiveElement.