1.0.11 • Published 11 months ago

@rsxt/react-listener v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

React Listener Hook

A lightweight and flexible React hook for handling event listeners with debounce, delay, and CSS selectors support.

šŸ“¦ Installation

yarn add react-listener
# or
npm install react-listener

šŸš€ Usage

Basic Example

import { useListener } from "react-listener";

function App() {
  useListener(window, "resize", () => {
    console.log("Window resized!");
  });

  return <div>Resize the window to see logs in the console.</div>;
}

Debounce Example

useListener(
  window,
  "scroll",
  () => {
    console.log("Scrolled!");
  },
  { debounce: 500 }
);

Using a Ref

import { useRef } from "react";
import { useListener } from "react-listener";

function ClickCounter() {
  const btnRef = useRef(null);
  const [count, setCount] = useState(0);

  useListener(btnRef, "click", () => setCount((prev) => prev + 1));

  return <button ref={btnRef}>Clicked {count} times</button>;
}

šŸ›  Features

āœ… Supports Multiple Events (click, keydown, etc.)
āœ… Debounce and Delay Support
āœ… Attach Listeners to Refs or Window/Document
āœ… CSS Selector Support (for dynamically added elements)
āœ… Automatic Cleanup on Unmount

šŸ“ License

This project is licensed under the MIT License.

1.0.11

11 months ago

1.0.10

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago