1.0.11 • Published 8 months ago

@rsxt/react-listener v1.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
8 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

8 months ago

1.0.10

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago