Skip to content

@tmrw-realityos/charmDocs


@tmrw-realityos/charm / on

Function: on()

on<T>(el, evt, cb): RemoveEventListener

Utility function to register and unregister event handlers in the DOM.

Normally, many things can go wrong in the traditional pattern of addEventListener and removeEventListener. If the function ref being passed to removeEventListener is not exactly identical to the function passed to addEventListener, the callback will not be removed and thus cause a leak. This pattern takes care of this issue.

Encourages a naming convention of "on" to register an event listener and "off" to remove it.

Type Parameters

T extends Element

Parameters

el: T

evt: string

cb

Returns

RemoveEventListener

Defined in

packages/charm/src/helpers/events.ts:11