createEventListener
Attach typed DOM event listeners that clean up automatically when the Solid owner scope disposes.
createEventListener and createEventListenerMap attach DOM event listeners to Window, Document, or HTML elements. They automatically register onCleanup() in the active SolidJS reactive scope to prevent memory leaks.
Import#
Signatures#
Usage Example#
Single Event with createEventListener#
src/EscapeListener.tsx
Multiple Events with createEventListenerMap#
src/DragObserver.tsx
Features#
- Type Inferred: Event types automatically narrow according to the target (e.g.
window.resizegivesUIEvent,document.keydowngivesKeyboardEvent). - Owner Scope Bound: Cleanups are registered with
onCleanupin the active Solid owner, including when called fromonMount. - Safe Null Handling: Passes
nullorundefinedtargets safely without runtime exceptions.