1.1.0 • Published 6 years ago
@hookstapose/use-event v1.1.0
useEvent
This hook lets you register an event on window or a passed element when the component mounts on the DOM and unregister it when the component unmounts from the DOM with the same API as the addEventListener method
Installation
Install with yarn:
yarn add @react-hook-utils/use-eventInstall with npm:
npm install @react-hook-utils/use-eventAPI
import {useEvent} from '@react-hook-utils/use-event'
useEvent('click', () => {
console.log('clicked')
})event: (Required) Eventcallback: (Required) Functionoptions: Boolean | Event Optionselement: HTMLElement
Usage
import React, {useState} from 'react'
import {useEvent} from '@react-hook-utils/use-event'
function Example() {
const [count, setCount] = useState(0)
useEvent('click', () => {
setCount(count + 1)
})
return (
<div>{count}</div>
)
}1.1.0
6 years ago