
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
@custom-react-hooks/use-event-listener
Advanced tools
The `useEventListener` hook is a custom React hook that simplifies the process of adding and removing event listeners in your React components. It handles the lifecycle of the event listener, ensuring it is cleaned up when the component unmounts or depend
The useEventListener hook is a custom React hook that simplifies the process of adding and removing event listeners in your React components. It handles the lifecycle of the event listener, ensuring it is cleaned up when the component unmounts or dependencies change.
npm install @custom-react-hooks/use-event-listener
or
yarn add @custom-react-hooks/use-event-listener
npm install @custom-react-hooks/all
or
yarn add @custom-react-hooks/all
Effortless Event Handling: Streamlines the process of adding and removing event listeners in React components, making it easier to handle user interactions and other events.
Automatic Cleanup: Automatically cleans up event listeners when the component unmounts or dependencies change, preventing memory leaks and ensuring optimal performance.
Support for Multiple Events: Capable of attaching listeners to multiple events simultaneously, making it versatile for handling various types of interactions within a single component.
Flexible Element Targeting: Offers the ability to attach event listeners to specific DOM elements using a ref, or defaults to the global window object if no element is specified.
Conditional Event Listening: Includes an optional condition parameter to dynamically attach or detach event listeners based on certain conditions, enhancing the hook's adaptability in different scenarios.
Custom Event Handling Function: Allows for the specification of a custom handler function for each event, providing full control over the response to the triggered events.
Option Customization: Supports both boolean and object options for event listeners, aligning with the standard addEventListener API and providing flexibility for more complex event handling requirements.
Ref Management: Uses a ref to store the current handler function, ensuring that the most recent handler is used and preventing stale closures.
Event Listener Abstraction: Abstracts the complexities of correctly attaching and detaching event listeners in React's lifecycle, allowing developers to focus on the core logic of their components.
Versatility in Use: Suitable for a wide range of use cases, from handling user inputs like keyboard and mouse events to responding to browser and device events like resizing and orientation changes.
Here's an example of how to use the useEventListener hook in a component:
import { useEventListener } from '@custom-react-hooks/all';
const EventListenerComponent = () => {
const [count, setCount] = useState(0);
const handleKeyDown = (event) => {
if (event.key === 'ArrowRight') {
setCount((prevCount) => prevCount + 1);
} else if (event.key === 'ArrowLeft') {
setCount((prevCount) => prevCount - 1);
}
};
useEventListener('keydown', handleKeyDown);
const getColor = () => {
if (count < 0) return '#ff5868';
if (count > 0) return '#00989a';
return '#f9f871';
};
return (
<div>
<h2>Press the Arrow Left/Right keys to change the counter.</h2>
<p>(If is not working, click me!)</p>
<p>
Counter: <span style={{ color: getColor() }}>{count}</span>
</p>
</div>
);
};
export default EventListenerComponent;
In this component, useEventListener is used to listen for keydown events on the div element, and the state is updated with the last key pressed.
eventName (string | string[]): The name of the event to listen to, or an array of event names.handler (function): The function to be called when the event is triggered.element (RefObject, optional): The ref object pointing to the DOM element to which the event listener will be attached. If not provided, the event listener will be attached to the window object.options (boolean | AddEventListenerOptions, optional): Options to pass to the event listener.condition (boolean, optional): A boolean value to conditionally attach or detach the event listener.Let's explore the use cases for each of the four additional hooks you've provided:
useEventListenerelement is mounted when the hook is called.Your contributions to the development and enhancement of this hook are welcome! Feel free to submit issues or pull requests to the repository.
FAQs
The `useEventListener` hook is a custom React hook that simplifies the process of adding and removing event listeners in your React components. It handles the lifecycle of the event listener, ensuring it is cleaned up when the component unmounts or depend
The npm package @custom-react-hooks/use-event-listener receives a total of 503 weekly downloads. As such, @custom-react-hooks/use-event-listener popularity was classified as not popular.
We found that @custom-react-hooks/use-event-listener demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.