• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A utility component to allow easy access to browser native events

react-native-listener npm version npm downloads

A utility component to allow easy access to browser native events.

THIS IS UNRELATED TO react-native!

Please don't confuse this library with anything to do with React Native. This library is for dealing directly with browser native events.

Why?

React's uses event delegation with a single event listener on document. While this is great if your entire application is inside React, it's not so great if your React component is inserted into a page containing other event listeners. If an event happens inside your React component, your component will be the last to hear of the event. The event will first propagate to all its ancestor elements on the page. Here is a Codesandbox to demonstrate.

If your problem is that you need to stop events leaking out of your React component to the rest of the page, <NativeListener> is the solution.

Installation

In your project dir:

npm install --save react-native-listener

Usage

In your JSX file, simply wrap the element (only one!) you want to listen to with <NativeListener> and put your event listener properties (e.g. onClick, onKeyDown) on <NativeListener> instead of on your element.

So, instead of this...

import React, {Component} from 'react';

export default class MyComponent extends Component {
  handleButtonClick(event) {
    // do something (event is React's SyntheticEvent)
  }

  render() {
    return (
      <div>
        <button onClick={this.handleButtonClick.bind(this)}>Click Me!</button>
      </div>
      );
  }
}

...do this:

import React, {Component} from 'react';
import NativeListener from 'react-native-listener';

export default class MyComponent extends Component {
  handleButtonClick(event) {
    // do something (event is native browser event)
  }

  render() {
    return (
      <div>
        <NativeListener onClick={this.handleButtonClick.bind(this)}>
          <button>Click Me!</button>
        </NativeListener>
      </div>
      );
  }
}

IMPORTANT: The event passed to your function is the native browser event, NOT React's SyntheticEvent!!

Convenience Properties

If all you want to do is stop the propagation of an event, there are convenience properties to do that. stopClick, stopKeyDown. For example, say you wanted to allow normal hyperlinks to work, but your component is inside some element that JQuery is calling event.preventDefault() for clicks...

import React, {Component} from 'react';
import NativeListener from 'react-native-listener';

export default class MyComponent extends Component {
  render() {
    return (
      <div>
        <NativeListener stopClick>
          <a href="https://github.com/erikras/react-native-listener">Check out this awesome code!</a>
        </NativeListener>
      </div>
      );
  }
});

IMPORTANT: You cannot just put a <NativeListener stopClick> surrounding your whole component and expect regular React events to work inside it. That will also prevent the clicks from bubbling up to React's event system listening on the document. If you block an event, you must use <NativeListener> to listen to that event everywhere inside the <NativeListener> element that is blocking the event.

Note on 1.0.2 Update

If you use react-native-listener >= 1.0.2 in CommonJS environment, don’t forget to add .default to your import:

- var NativeListener = require('react-native-listener')
+ var NativeListener = require('react-native-listener').default

Advanced Usage

By default, the onClick, onKeyDown event listeners fire on bubble. If you understand the difference between bubble and capture and you really need to listen on capture, you can simply append Capture to your event property. e.g. onClickCapture, onKeyDownCapture.


Module written by Erik Rasmussen @erikras

More Repositories

1

react-redux-universal-hot-example

A starter boilerplate for a universal webapp using express, react, redux, webpack, and react-transform
JavaScript
12,008
star
2

ducks-modular-redux

A proposal for bundling reducers, action types and actions when using Redux
JavaScript
9,586
star
3

redux-form-material-ui

A set of wrapper components to facilitate using Material UI with Redux Form
JavaScript
831
star
4

multireducer

A utility to wrap many copies of a single Redux reducer into a single key-based reducer.
JavaScript
423
star
5

lru-memoize

A utility to provide LRU memoization for any js function
JavaScript
317
star
6

styled-components-theme

Defines themes via flexible color selectors for use with styled-components
JavaScript
306
star
7

remix-conf-2022

XState on the backend!
CSS
107
star
8

react-pdfjs

A React component to wrap PDF.js
JavaScript
81
star
9

react-redux-promise-listener

A React component and Redux middleware that allows actions to be converted into Promises
JavaScript
71
star
10

audiocard

⏯️ AudioCard - Opinionated, responsive, audio player compatible with Twitter Cards
TypeScript
66
star
11

map-props

A higher order React component and allows mapping prop values passed in to other prop values expected by the wrapped component
JavaScript
45
star
12

react-lazy-cache

A utility to lazily calculate and cache values in a react component based on props
JavaScript
45
star
13

redux-promise-listener

A Redux middleware that allows actions to be converted into Promises
JavaScript
44
star
14

reactalicante2018

Code from my talk at React Alicante 2018
CSS
29
star
15

redux-spy

A higher order component decorator to read from a Redux store without subscribing to all its changes
JavaScript
29
star
16

reactalicante2017

Code used during my Redux Form talk at React Alicante 2017
CSS
26
star
17

react-callbag-listener

πŸ‘‚ A React render-prop component that listens to values emitted by callbags
JavaScript
21
star
18

tic-tac-toe-finite-state-machine

An exercise to build a tic-tac-toe finite state machine in typescript and xstate
TypeScript
17
star
19

callbag-pausable

⏯️ Callbag Pausable is a callbag that will convert any callbag stream into one that can be paused and resumed via data or talkback events.
JavaScript
10
star
20

react-thug

Silly React "Thug Life" meme image
10
star
21

postmessage-client-server

A simple promise-based client and server to communicate between pages and iframes with postmessage.
JavaScript
9
star
22

happyhourfm

Happy Hour with Dennis and Erik
TypeScript
6
star
23

expect-predicate

An extension for expect that lets you test values against arbitrary predicates
JavaScript
6
star
24

reactathon2020

Modern Forms in React workshop for Reactathon 2020
JavaScript
5
star
25

potionocean

TypeScript
5
star
26

javascript-flickr-badge

Javascript Flickr Badge
4
star
27

lemoncoders-20180502

Material de una clase he dado para Lemon Coders
JavaScript
4
star
28

seekjusticefm

Seek Justice Podcast
TypeScript
4
star
29

primatrix

If x is prime, is x + 30n prime?
TypeScript
3
star
30

callbag-types

Types for πŸ‘œ Callbags
JavaScript
3
star
31

reactfinland2020

JavaScript
2
star
32

typescript-react-apollo-example

Just demonstrating a problem... move along...
1
star
33

datocms-virtual-event-starter-kit-demo-8411

TypeScript
1
star
34

react-hot-loader-problem

A demonstration of a problem (bug?) in react-hot-loader
JavaScript
1
star
35

rescript-gentype-bug

Reproduction of a problem with Rescript's genType
TypeScript
1
star