@hookstapose/use-event
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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-event

Install with npm:

npm install @react-hook-utils/use-event

API

import {useEvent} from '@react-hook-utils/use-event'

useEvent('click', () => {
    console.log('clicked')
})
  • event: (Required) Event
  • callback: (Required) Function
  • options: Boolean | Event Options
  • element: 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>
    )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @hookstapose/use-event

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

4.51 kB

Total Files

6

Last publish

Collaborators

  • maanuchi
  • beogip