![New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions](https://cdn.sanity.io/images/cgdhsj6q/production/86e6ebdea652d20da070ebbda20134b839972db7-1024x1024.webp?w=800&fit=max&auto=format)
Security News
New axobject-query Maintainer Faces Backlash Over Controversial Decision to Support Legacy Node.js Versions
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
@react-hook/event
Advanced tools
Readme
npm i @react-hook/event
A React hook for adding events to HTML elements. This hook cleans up your listeners
automatically when it unmounts. You won't have to worry about wrapping your
listener in a useCallback()
because this hook makes sure your most recent callback
is always invoked.
import * as React from 'react'
import useEvent from '@react-hook/event'
// Logs an event each time target.current is clicked
const Component = () => {
const target = useRef(null)
useEvent(target, 'click', (event) => console.log(event))
return <div ref={target} />
}
// Logs an event each time the `document` is clicked
const useLogDocumentClick = () => {
useEvent(document, 'click', (event) => console.log(event))
}
// Logs an event each time the `window` is clicked
const useLogWindowClick = () => {
useEvent(window, 'click', (event) => console.log(event))
}
// Logs an event each time element#foo` is clicked
const useLogElementClick = () => {
useEvent(document.getElementById('foo'), 'click', (event) =>
console.log(event)
)
}
const useEvent = <
// Also has Window, Document overloads
T extends HTMLElement = HTMLElement,
K extends keyof HTMLElementEventMap = keyof HTMLElementEventMap
>(
target: React.RefObject<T> | T | Window | Document | null,
type: K,
listener: EventListener<K>,
cleanup?: (...args: any[]) => any
)
Argument | Type | Required? | Description |
---|---|---|---|
target | React.RefObject<T> | T | Window | Document | null | Yes | The React ref, window , or document to add the event listener to |
type | keyof EventMap | Yes | The type of event to listen for |
listener | (this: T, ev: EventMap[K]) => any | Yes | The callback invoked when the event type fires |
cleanup | (...args: any[]) => any | No | This callback will be invoked when the event unmounts. This is in addition to the automatic event listener cleanup that occurs. A common use case could be something like clearing a timeout. |
MIT
FAQs
A React hook for managing event listeners, e.g. removing events when a component unmounts.
The npm package @react-hook/event receives a total of 138,308 weekly downloads. As such, @react-hook/event popularity was classified as popular.
We found that @react-hook/event 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
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.
Security News
Results from the 2023 State of JavaScript Survey highlight key trends, including Vite's dominance, rising TypeScript adoption, and the enduring popularity of React. Discover more insights on developer preferences and technology usage.
Security News
The US Justice Department has penalized two consulting firms $11.3 million for failing to meet cybersecurity requirements on federally funded projects, emphasizing strict enforcement to protect sensitive government data.