LIGHTWEIGHT TEMPLATES
For Lightweight templates, you can use a new API to subscribe to events. Previously, to subscribe to button-click events, you had to take an element from the DOM, register an event subscription, and then unsubscribe if the element is removed from the DOM.
Old way:
<div id="elementId">html element</div>
<custom-script>
const elementVariable = document.querySelector('#elementId');
elementVariable.addEventListener('click', clickHandler);
const clickHandler = (event) => {
// handler logic
}
// and if this element is destroyed in the future, you need to remove the listener.
elementVariable.removeEventListener('click', clickHandler);
</custom-script>
New way:
<div id="elementId" onClick={(event) => clickHandler(event)}>html element</div>
// JS tab
const clickHandler = (event) => {
// handler logic
}
In the new way, you don't need to unsubscribe from the event.
Most Commonly Used Events
-
onClick- Click handler -
onInput- Input value changes -
onChange- Form element changes -
onSubmit- Form submission -
onKeyDown- Key press handling -
onMouseEnter/onMouseLeave- Hover effects -
onFocus/onBlur- Focus management
List of All Events
The table is not exhaustive: on an HTML element, any attribute that starts with on listens for the DOM event named by the rest of the attribute in lower case, so onAnimationEnd listens for animationend.
|
Category |
JSX Handler |
Underlying DOM Event(s) |
|---|---|---|
|
Mouse |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Wheel/Scroll |
|
|
|
|
|
|
|
Form |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Keyboard |
|
|
|
|
|
|
|
|
|
|
|
Focus |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Drag |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Touch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Pointer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Media |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Other |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|