Socket
Socket
Sign inDemoInstall

@use-it/event-listener

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@use-it/event-listener

A custom React Hook that provides a useEventListener.


Version published
Weekly downloads
116K
increased by2.41%
Maintainers
1
Weekly downloads
 
Created

What is @use-it/event-listener?

@use-it/event-listener is a React hook for adding and cleaning up event listeners in a declarative way. It simplifies the process of attaching event listeners to DOM elements or the window object, ensuring that they are properly cleaned up when the component unmounts.

What are @use-it/event-listener's main functionalities?

Window Event Listener

This feature allows you to add an event listener to the window object. In this example, a 'resize' event listener is added to the window, and the handleResize function is called whenever the window is resized.

import { useEffect } from 'react';
import useEventListener from '@use-it/event-listener';

function WindowResizeComponent() {
  const handleResize = () => {
    console.log('Window resized');
  };

  useEventListener('resize', handleResize, window);

  return <div>Resize the window and check the console</div>;
}

Element Event Listener

This feature allows you to add an event listener to a specific DOM element. In this example, a 'click' event listener is added to a button element, and the handleClick function is called whenever the button is clicked.

import { useRef } from 'react';
import useEventListener from '@use-it/event-listener';

function ClickableComponent() {
  const buttonRef = useRef(null);

  const handleClick = () => {
    console.log('Button clicked');
  };

  useEventListener('click', handleClick, buttonRef.current);

  return <button ref={buttonRef}>Click me</button>;
}

Custom Event Listener

This feature allows you to add a custom event listener. In this example, a 'customEvent' listener is added to the window, and the handleCustomEvent function is called whenever the custom event is dispatched.

import { useEffect } from 'react';
import useEventListener from '@use-it/event-listener';

function CustomEventComponent() {
  const handleCustomEvent = (event) => {
    console.log('Custom event triggered', event.detail);
  };

  useEventListener('customEvent', handleCustomEvent, window);

  useEffect(() => {
    const event = new CustomEvent('customEvent', { detail: 'Hello, world!' });
    window.dispatchEvent(event);
  }, []);

  return <div>Check the console for custom event details</div>;
}

Other packages similar to @use-it/event-listener

Keywords

FAQs

Package last updated on 22 Nov 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc