rad-event-listener

v0.1.0
Simple wrapper for addEventListener that returns a cleanup function so you don't have to call removeEventListener manually. The rad part is that it works with typescript 🤯
addEventListener cleanup typescript

Rad Event Listener

minzip size install size dependency count

Please see the full README at https://github.com/JLarky/rad-event-listener

Before:

function handler(this: Document, e: MouseEvent) {
  console.log("mouse moved to", e.x, e.y, this === e.currentTarget);
};

document.addEventListener("mousemove", handler);

const cleanup = () => {
  document.removeEventListener("mousemove", handler);
};

After:

import { on, rad, radEventListener } from "rad-event-listener";

const cleanup = radEventListener(document, "mousemove", function (e) {
  console.log("mouse moved to", e.x, e.y, this === e.currentTarget);
});

Both of examples are written in a type-safe manner that will not allow you to make mistakes. But one of them made you work much more to get types of this and e right as well as made you do more work to remove the listener.

Live examples

Metadata

  • MIT
  • >=8.0.0
  • Unknown
  • released 6/5/2023

Downloads

Maintainers