Reactive element
When you want to create a reactive WebApp, you need elements that can react(re-render) to different inputs (attribute/property).
Definition
Define reactive Attribute, using decorator @attribute:
In the above example, the field firstName of MyElement is declared as a reactive property.
When this property change, the mounted instance of MyElement will re-render,
additionally, this field maps to the element's first-name Attribute.
Similar to @attribute, GemElement also provides numattribute boolattribute to support Number and Boolean values. And @property is used to reflect the specified Property:
TIP
- Do not modify prop/attr within the element, they should be passed in one-way by the parent element, just like native elements
GemElementextends fromHTMLElement, don’t override the attribute/property/method/event, use private fields to avoid overwriting the property/methods ofGemElement/HTMLElement
In addition, Gem provides the createState API to create the element's own state, and the created state object also acts as an update function, triggering the element to re-render when called.
Example
Life cycle
WARNING
The lifecycle may be replaced in the future by decorators based on
@effect@memo@willMount@template@mounted@unmounted
You can specify life cycle functions for GemElement. Sometimes they are useful, for example:
Complete life cycle:
NOTE
The
constructorandunmountedof the parent element are executed before the child element, but themountedis executed after the child element