This can be achieved with a Lit feature called Reactive Properties. Define a reactive property words:
classWordViewerextendsLitElement {
@property() words='initial value';
...
}
classWordViewerextendsLitElement {
staticproperties = {
words: {},
};
constructor() {
super();
this.words='initial value';
}
...
}
Lit Reactive Properties are special properties that can interact with Lit's render lifecycle. If the reactive property words changes, the component schedules an update. By default, Lit sets up attribute synchronization for the property and will update the property when the attribute changes.
You should now see initial value in the preview.
Before moving on, change to the index.html file and set words from an attribute.
<!-- index.html -->
<word-viewerwords="👋.from.html"></word-viewer>
The preview should now show 👋.from.html.
Debugging Custom Elements is awesome!
Custom Elements are like any other HTML Element, so your browser's development
tools already understand how to handle them!
Change the words attribute in your browser's developer tools and note that the
component reacts to the change.