rad-event-listener
v0.2.4
Published
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 🤯
Downloads
496
Maintainers
Readme
Rad Event Listener
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.