react-event-listener

WebJar for react-event-listener

License

License

MIT
Categories

Categories

React User Interface Web Frameworks
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

react-event-listener
Last Version

Last Version

0.6.6
Release Date

Release Date

Type

Type

jar
Description

Description

react-event-listener
WebJar for react-event-listener
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/oliviertassinari/react-event-listener

Download react-event-listener

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/react-event-listener/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>react-event-listener</artifactId>
    <version>0.6.6</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/react-event-listener/
implementation 'org.webjars.npm:react-event-listener:0.6.6'
// https://jarcasting.com/artifacts/org.webjars.npm/react-event-listener/
implementation ("org.webjars.npm:react-event-listener:0.6.6")
'org.webjars.npm:react-event-listener:jar:0.6.6'
<dependency org="org.webjars.npm" name="react-event-listener" rev="0.6.6">
  <artifact name="react-event-listener" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='react-event-listener', version='0.6.6')
)
libraryDependencies += "org.webjars.npm" % "react-event-listener" % "0.6.6"
[org.webjars.npm/react-event-listener "0.6.6"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm : babel__runtime jar [7.2.0,8)
org.webjars.npm : prop-types jar [15.6.0,16)
org.webjars.npm : warning jar [4.0.1,5)

Project Modules

There are no modules declared in this project.

react-event-listener

A React component for binding events on the global scope.

npm version npm downloads Build Status

Dependencies DevDependencies

Installation

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

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 issue from React, TestUtils.Simulate. methods won't bubble up to window or document. As a result, you must use document.dispatchEvent or simulate event using native DOM api.

See our test cases for more information.

License

MIT

Versions

Version
0.6.6
0.6.4
0.6.3
0.6.2
0.5.10
0.5.3
0.4.5
0.4.0
0.3.1
0.2.1
0.1.1