Reactive Properties

Permalink to "Reactive Properties"

Lit introduces a set of render lifecycle callback methods on top of the native web component callbacks. These callbacks are triggered when declared Lit reactive properties are changed.

To learn more about the Lit reactive update cycle, see the Lit Lifecycle documentation.

To use this feature, you must statically declare which properties are Reactive Properties – properties that will trigger the render lifecycle when changed:

rating-element.

Permalink to "rating-element."

Here, you:

Pass complex objects as properties.

It is generally good practice to pass complex objects as properties rather than attributes. Read more on reactive property attribute conversion in the Lit documentation.

Additionally, the reflect flag on the vote property will automatically update the host element's vote attribute that you formerly updated in the vote setter. It is necessary to reflect the vote attribute so the :host([vote=up]) styles can be applied.

willUpdate Lifecycle Callback

Permalink to "willUpdate Lifecycle Callback"

Now update the rating when the vote property changes in the willUpdate() Lit lifecycle method:

rating-element.

Permalink to "rating-element."

The logic here is the same from the vote setter logic just moved to to the willUpdate() lifecycle method.

The willUpdate() method is called before render() every time a reactive property is changed. Because LitElement batches property changes and makes rendering asynchronous, changes to reactive properties (like this.rating) in willUpdate() will not trigger unnecessary render lifecycle calls.

Congratulations, you should have a working Lit Element!