# react-event-listener

> A React component that allow to bind events on the global scope

Latest version **0.6.7** (published 2025-09-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-event-listener
pnpm add react-event-listener
yarn add react-event-listener
bun add react-event-listener
```

## Health

**Score 23/100 (F)** — status: maintenance-mode.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: stale; low maintenance score; declining downloads.

## Facts

| | |
|---|---|
| Version | 0.6.7 |
| Published | 2025-09-20 |
| First published | 2015-06-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-event-listener) |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 32.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 354 |
| Author | Olivier Tassinari |
| Maintainers | oliviertassinari |
| Keywords | react, event, listener, binding |

## Links

- npm: https://www.npmjs.com/package/react-event-listener
- Repository: https://github.com/oliviertassinari/react-event-listener
- Issues: https://github.com/oliviertassinari/react-event-listener/issues
- npm.io page: https://npm.io/package/react-event-listener

## Dependencies (3)

- [warning](https://npm.io/package/warning.md) ^4.0.1
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.2.0

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 0.6.7 (latest) — 2025-09-20
- 0.6.6 — 2019-02-02
- 0.6.5 — 2018-12-27
- 0.6.4 — 2018-09-22
- 0.6.3 — 2018-08-20
- 0.5.10 — 2018-08-07
- 0.6.2 — 2018-08-04
- 0.6.1 — 2018-06-15
- 0.6.0 — 2018-05-27
- 0.5.9 — 2018-05-23
- 0.5.8 — 2018-05-22
- 0.5.7 — 2018-05-22
- 0.5.6 — 2018-05-21
- 0.5.5 — 2018-05-20
- 0.5.4 — 2018-05-20
- … 19 more at https://npm.io/package/react-event-listener/versions

## README

# react-event-listener

> A React component for binding events on the global scope.

[![npm version](https://img.shields.io/npm/v/react-event-listener.svg?style=flat-square)](https://www.npmjs.com/package/react-event-listener)
[![npm downloads](https://img.shields.io/npm/dm/react-event-listener.svg?style=flat-square)](https://www.npmjs.com/package/react-event-listener)
[![Build Status](https://travis-ci.org/oliviertassinari/react-event-listener.svg?branch=master)](https://travis-ci.org/oliviertassinari/react-event-listener)

[![Dependencies](https://img.shields.io/david/oliviertassinari/react-event-listener.svg?style=flat-square)](https://david-dm.org/oliviertassinari/react-event-listener)
[![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&view=list)

## Installation

```sh
npm install react-event-listener
```

## The problem solved

This module provides a **declarative way** to bind events to a global `EventTarget`.
It's using the React lifecycle to bind and unbind at the right time.

## Usage

```js
import React, {Component} from 'react';
import EventListener, {withOptions} from 'react-event-listener';

class MyComponent extends Component {
  handleResize = () => {
    console.log('resize');
  };

  handleScroll = () => {
    console.log('scroll');
  };

  handleMouseMove = () => {
    console.log('mousemove');
  };

  render() {
    return (
      <div>
        <EventListener
          target="window"
          onResize={this.handleResize}
          onScroll={withOptions(this.handleScroll, {passive: true, capture: false})}
        />
        <EventListener target={document} onMouseMoveCapture={this.handleMouseMove} />
      </div>
    );
  }
}
```

### Note on server-side rendering

When doing server side rendering, `document` and `window` aren't available.
You can use a string as a `target`, or check that they exist before rendering
the component.

### Note on performance

You should avoid passing inline functions for listeners, because this creates a new `Function` instance on every
render, defeating `EventListener`'s `shouldComponentUpdate`, and triggering an update cycle where it removes its old
listeners and adds its new listeners (so that it can stay up-to-date with the props you passed in).

### Note on testing

In [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).

See our [test cases](https://github.com/oliviertassinari/react-event-listener/blob/master/src/index.spec.js) for more information.

## License

MIT

---
_Source: https://npm.io/package/react-event-listener · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
