{"id":13680255,"url":"https://github.com/oliviertassinari/react-event-listener","last_synced_at":"2025-04-29T23:30:57.472Z","repository":{"id":33451080,"uuid":"37096474","full_name":"oliviertassinari/react-event-listener","owner":"oliviertassinari","description":"A React component for binding events on the global scope. :dizzy:","archived":true,"fork":false,"pushed_at":"2019-02-02T11:22:02.000Z","size":467,"stargazers_count":357,"open_issues_count":6,"forks_count":36,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-22T22:07:33.328Z","etag":null,"topics":["binding","event","listener","react"],"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/oliviertassinari.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-08T22:31:30.000Z","updated_at":"2024-09-16T10:34:07.000Z","dependencies_parsed_at":"2022-09-08T11:50:22.015Z","dependency_job_id":null,"html_url":"https://github.com/oliviertassinari/react-event-listener","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Freact-event-listener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Freact-event-listener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Freact-event-listener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Freact-event-listener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oliviertassinari","download_url":"https://codeload.github.com/oliviertassinari/react-event-listener/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251599824,"owners_count":21615586,"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":["binding","event","listener","react"],"created_at":"2024-08-02T13:01:14.907Z","updated_at":"2025-04-29T23:30:53.320Z","avatar_url":"https://github.com/oliviertassinari.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# react-event-listener\n\n\u003e A React component for binding events on the global scope.\n\n[![npm version](https://img.shields.io/npm/v/react-event-listener.svg?style=flat-square)](https://www.npmjs.com/package/react-event-listener)\n[![npm downloads](https://img.shields.io/npm/dm/react-event-listener.svg?style=flat-square)](https://www.npmjs.com/package/react-event-listener)\n[![Build Status](https://travis-ci.org/oliviertassinari/react-event-listener.svg?branch=master)](https://travis-ci.org/oliviertassinari/react-event-listener)\n\n[![Dependencies](https://img.shields.io/david/oliviertassinari/react-event-listener.svg?style=flat-square)](https://david-dm.org/oliviertassinari/react-event-listener)\n[![DevDependencies](https://img.shields.io/david/dev/oliviertassinari/react-event-listener.svg?style=flat-square)](https://david-dm.org/oliviertassinari/react-event-listener#info=devDependencies\u0026view=list)\n\n## Installation\n\n```sh\nnpm install react-event-listener\n```\n\n## The problem solved\n\nThis module provides a **declarative way** to bind events to a global `EventTarget`.\nIt's using the React lifecycle to bind and unbind at the right time.\n\n## Usage\n\n```js\nimport React, {Component} from 'react';\nimport EventListener, {withOptions} from 'react-event-listener';\n\nclass MyComponent extends Component {\n  handleResize = () =\u003e {\n    console.log('resize');\n  };\n\n  handleScroll = () =\u003e {\n    console.log('scroll');\n  };\n\n  handleMouseMove = () =\u003e {\n    console.log('mousemove');\n  };\n\n  render() {\n    return (\n      \u003cdiv\u003e\n        \u003cEventListener\n          target=\"window\"\n          onResize={this.handleResize}\n          onScroll={withOptions(this.handleScroll, {passive: true, capture: false})}\n        /\u003e\n        \u003cEventListener target={document} onMouseMoveCapture={this.handleMouseMove} /\u003e\n      \u003c/div\u003e\n    );\n  }\n}\n```\n\n### Note on server-side rendering\n\nWhen doing server side rendering, `document` and `window` aren't available.\nYou can use a string as a `target`, or check that they exist before rendering\nthe component.\n\n### Note on performance\n\nYou should avoid passing inline functions for listeners, because this creates a new `Function` instance on every\nrender, defeating `EventListener`'s `shouldComponentUpdate`, and triggering an update cycle where it removes its old\nlisteners and adds its new listeners (so that it can stay up-to-date with the props you passed in).\n\n### Note on testing\n\nIn [this](https://github.com/facebook/react/issues/5043) issue from React, `TestUtils.Simulate.` methods won't bubble up to `window` or `document`. As a result, you must use [`document.dispatchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent) or simulate event using [native DOM api](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click).\n\nSee our [test cases](https://github.com/oliviertassinari/react-event-listener/blob/master/src/index.spec.js) for more information.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliviertassinari%2Freact-event-listener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foliviertassinari%2Freact-event-listener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliviertassinari%2Freact-event-listener/lists"}