@property decorator creates a public reactive property on a LitElement class.
When the property changes, the element schedules an update and re-renders.
Public properties are observable from outside the element and can be set via HTML attributes or JavaScript.
Import
Signature
Options
Controls whether and how the property maps to an HTML attribute.
true(default): the lowercased property name is used as the attribute name (e.g.fooBar→foobar).- A string: uses that string as the attribute name (e.g.
attribute: 'foo-bar'). false: the property is not associated with any attribute.
A type hint used by the default converter to parse the attribute string into a
property value. Has no effect when a custom
converter is provided.When
true, the property value is serialized back to the corresponding HTML
attribute whenever the property changes.Customizes how the attribute value is converted to and from the property
value. Can be:
- A function
(value: string | null, type?) => Tfor attribute-to-property conversion only. - An object with optional
fromAttribute(value, type)andtoAttribute(value, type)methods.
A function that determines whether a property change should trigger an
update. By default, uses
notEqual which is !Object.is(value, oldValue) — stricter than !== because it handles NaN correctly. Return true to request an update.When
true, no getter/setter accessor is generated for the property.
You become responsible for calling this.requestUpdate(propertyName, oldValue)
manually when the property changes.When
true, marks the property as internal state. The property is not added
to observedAttributes. Prefer using the @state
decorator instead.When
true, the initial value of the property is treated as the default
value. The initial value does not reflect to the attribute, and when the
attribute is removed the property resets to its default. Properties that
reflect to attributes should generally set useDefault: true so their
initial values do not cause an initial reflection.Examples
Basic usage
Custom attribute name
Reflecting to an attribute
this.status is set to 'active', the element’s status attribute
is updated automatically, which enables CSS attribute selectors:
Custom converter
Without decorators
Use the staticproperties field to declare properties without decorators: