{"id":13451929,"url":"https://github.com/donavon/use-event-listener","last_synced_at":"2025-05-16T04:04:06.554Z","repository":{"id":34136402,"uuid":"170070402","full_name":"donavon/use-event-listener","owner":"donavon","description":"A custom React Hook that provides a declarative useEventListener","archived":false,"fork":false,"pushed_at":"2023-07-24T11:43:53.000Z","size":729,"stargazers_count":355,"open_issues_count":21,"forks_count":35,"subscribers_count":7,"default_branch":"develop","last_synced_at":"2024-05-17T18:03:16.879Z","etag":null,"topics":["event-listener","hooks","react","reactjs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/donavon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-11T04:58:29.000Z","updated_at":"2024-06-18T12:28:51.161Z","dependencies_parsed_at":"2024-06-18T12:28:35.710Z","dependency_job_id":"f139d710-8b03-47e3-a700-6857591198d7","html_url":"https://github.com/donavon/use-event-listener","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donavon%2Fuse-event-listener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donavon%2Fuse-event-listener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donavon%2Fuse-event-listener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donavon%2Fuse-event-listener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/donavon","download_url":"https://codeload.github.com/donavon/use-event-listener/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254464891,"owners_count":22075570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event-listener","hooks","react","reactjs"],"created_at":"2024-07-31T07:01:07.223Z","updated_at":"2025-05-16T04:04:06.535Z","avatar_url":"https://github.com/donavon.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# @use-it/event-listener\n\nA custom React Hook that provides a declarative useEventListener.\n\n[![npm version](https://badge.fury.io/js/%40use-it%2Fevent-listener.svg)](https://badge.fury.io/js/%40use-it%2Fevent-listener) [![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)\n\nThis hook was inspired by [Dan Abramov](https://github.com/gaearon)'s\nblog post\n[\"Making setInterval Declarative with React Hooks\"](https://overreacted.io/making-setinterval-declarative-with-react-hooks/).\n\nI needed a way to simplify the plumbing around adding and removing an event listener\nin a custom hook.\nThat lead to a [chain of tweets](https://twitter.com/donavon/status/1093612936621379584)\nbetween Dan and myself.\n\n## Installation\n\n```bash\n$ npm i @use-it/event-listener\n```\n\nor\n\n```bash\n$ yarn add @use-it/event-listener\n```\n\n## Usage\n\nHere is a basic setup.\n\n```js\nuseEventListener(eventName, handler, element, options);\n```\n\n### Parameters\n\nHere are the parameters that you can use. (\\* = optional)\n\n| Parameter   | Description                                                                                                                                                                                                                            |\n| :---------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `eventName` | The event name (string). Here is a list of [common events](https://developer.mozilla.org/en-US/docs/Web/Events).                                                                                                                       |\n| `handler`   | A function that will be called whenever `eventName` fires on `element`.                                                                                                                                                                |\n| `element`\\* | An optional element to listen on. Defaults to `global` (i.e., `window`).                                                                                                                                                               |\n| `options`\\* | An object `{ capture?: boolean, passive?: boolean, once?: boolean }` to be passed to `addEventListener`. For advanced use cases. See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) for details. |\n\n### Return\n\nThis hook returns nothing.\n\n## Example\n\nLet's look at some sample code. Suppose you would like to track the mouse\nposition. You _could_ subscribe to mouse move events with like this.\n\n```js\nconst useMouseMove = () =\u003e {\n  const [coords, setCoords] = useState([0, 0]);\n\n  useEffect(() =\u003e {\n    const handler = ({ clientX, clientY }) =\u003e {\n      setCoords([clientX, clientY]);\n    };\n    window.addEventListener('mousemove', handler);\n    return () =\u003e {\n      window.removeEventListener('mousemove', handler);\n    };\n  }, []);\n\n  return coords;\n};\n```\n\nHere we're using `useEffect` to roll our own handler add/remove event listener.\n\n`useEventListener` abstracts this away. You only need to care about the event name\nand the handler function.\n\n```js\nconst useMouseMove = () =\u003e {\n  const [coords, setCoords] = useState([0, 0]);\n\n  useEventListener('mousemove', ({ clientX, clientY }) =\u003e {\n    setCoords([clientX, clientY]);\n  });\n\n  return coords;\n};\n```\n\n## Live demo\n\nYou can view/edit the sample code above on CodeSandbox.\n\n[![Edit demo app on CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/k38lyx2q9o)\n\n## License\n\n**[MIT](LICENSE)** Licensed\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"http://donavon.com\"\u003e\u003cimg src=\"https://avatars3.githubusercontent.com/u/887639?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDonavon West\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#infra-donavon\" title=\"Infrastructure (Hosting, Build-Tools, etc)\"\u003e🚇\u003c/a\u003e \u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=donavon\" title=\"Tests\"\u003e⚠️\u003c/a\u003e \u003ca href=\"#example-donavon\" title=\"Examples\"\u003e💡\u003c/a\u003e \u003ca href=\"#ideas-donavon\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e \u003ca href=\"#maintenance-donavon\" title=\"Maintenance\"\u003e🚧\u003c/a\u003e \u003ca href=\"https://github.com/donavon/use-event-listener/pulls?q=is%3Apr+reviewed-by%3Adonavon\" title=\"Reviewed Pull Requests\"\u003e👀\u003c/a\u003e \u003ca href=\"#tool-donavon\" title=\"Tools\"\u003e🔧\u003c/a\u003e \u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=donavon\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/third774\"\u003e\u003cimg src=\"https://avatars3.githubusercontent.com/u/8732191?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eKevin Kipp\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=third774\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/wKovacs64\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/1288694?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJustin Hall\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=wKovacs64\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=wKovacs64\" title=\"Documentation\"\u003e📖\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/huan086\"\u003e\u003cimg src=\"https://avatars2.githubusercontent.com/u/1448788?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eJeow Li Huan\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/donavon/use-event-listener/pulls?q=is%3Apr+reviewed-by%3Ahuan086\" title=\"Reviewed Pull Requests\"\u003e👀\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"http://normanrz.com/\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/335438?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eNorman Rzepka\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"#ideas-normanrz\" title=\"Ideas, Planning, \u0026 Feedback\"\u003e🤔\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/bvanderdrift\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/6398452?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eBeer van der Drift\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=bvanderdrift\" title=\"Tests\"\u003e⚠️\u003c/a\u003e \u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=bvanderdrift\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/pruge\"\u003e\u003cimg src=\"https://avatars1.githubusercontent.com/u/5827473?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eclingsoft\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/donavon/use-event-listener/commits?author=pruge\" title=\"Code\"\u003e💻\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonavon%2Fuse-event-listener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdonavon%2Fuse-event-listener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonavon%2Fuse-event-listener/lists"}