Lit's reactive properties system allows you to define properties on your custom element that automatically update the element when their values change.
String
: a string valueNumber
: a numeric valueBoolean
: a boolean valueArray
: an array valueObject
: an object value
attribute
: the name of the HTML attribute to bind the property toreflect
: whether or not to reflect the property value to the attributeconverter
: a function to convert the attribute value to the property valuehasChanged
: a function that determines if the property has changedtype
: the data type of the propertystate
: Set to true to declare the property as internal reactive state. Internal reactive state triggers updates like public reactive properties, but Lit doesn't generate an attribute for it, and users shouldn't access it from outside the component. Equivalent to using the@state
decorator.noAccessor
: set to true to avoid generating the default property accessors.
Expressions allow you to insert dynamic content into your template. You can use expressions by wrapping JavaScript code in ${}
:
import { LitElement, html, property } from 'lit-element';
class MyElement extends LitElement {
@property({ type: String }) name = '';
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
You can use expressions to bind values to form elements, such as inputs and selects. To bind a value to an input, you can use the .value
property:
import { LitElement, html, property } from 'lit-element';
class MyElement extends LitElement {
@property({ type: String }) name = '';
render() {
return html`
<label for="name-input">Name:</label>
<input id="name-input" .value=${this.name} @input=${this.handleInput} />
`;
}
handleInput(event: Event) {
this.name = (event.target as HTMLInputElement).value;
}
}
In some cases, you may want to use the ?
prefix on the property name instead of the .value
syntax. This is useful when you want to bind to a boolean value.
import { LitElement, html, property } from 'lit-element';
class MyElement extends LitElement {
@property({ type: Boolean }) checked = false;
render() {
return html`
<input type="checkbox" ?checked=${this.checked} @change=${this.handleChange} />
`;
}
handleChange(event: Event) {
this.checked = (event.target as HTMLInputElement).checked;
}
}
To handle events, you can use the @event
decorator to define a custom event:
import { LitElement, html } from 'lit';
class MyElement extends LitElement {
@event({ type: 'my-event' }) myEvent;
handleClick() {
this.dispatchEvent(new CustomEvent('my-event', { detail: { message: 'Hello!' } }));
}
render() {
return html`
<button @click=${this.handleClick}>Click me</button>
`;
}
}
Lit components use the standard custom element lifecycle methods. In addition Lit introduces a reactive update cycle that renders changes to DOM when reactive properties change.
constructor()
: Called when an element is created.connectedCallback()
: Called when the element is added to the DOM.disconnectedCallback()
: Called when the element is removed from the DOM.attributeChangedCallback(name, oldVal, newVal)
: Called when an observed attribute's value changes.adoptedCallback()
: Invoked when a component is moved to a new document.
shouldUpdate(changedProps)
: Called before rendering to determine if the element should update. changedProps is an object containing the names and values of the properties that have changed since the last render.willUpdate(changedProps)
: Called beforeupdate()
to compute values needed during the update.update(changedProps)
: Called to update the element's render tree. changedProps is an object containing the names and values of the properties that have changed since the last render.render()
: Called to render the element's template. Should return a TemplateResult object.
After update()
is called to render changes to the component's DOM, you can perform actions on the component's DOM using these methods.
firstUpdated(changedProps)
: Called after the component's DOM has been updated the first time.updated(changedProps)
: Called whenever the component’s update finishes and the element's DOM has been updated and rendered.
For more information, see the Lit documentation.