All Projects → erikras → React Native Listener

erikras / React Native Listener

Licence: mit
A utility component to allow easy access to browser native events

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Native Listener

Sysend.js
Send messages between open pages or tabs in same browser
Stars: ✭ 347 (+155.15%)
Mutual labels:  events, browser
Is Hotkey
Check whether a browser event matches a hotkey.
Stars: ✭ 211 (+55.15%)
Mutual labels:  events, browser
Noel
A universal, human-centric, replayable javascript event emitter.
Stars: ✭ 158 (+16.18%)
Mutual labels:  events, browser
Capture Frame
Capture video screenshot from a `<video>` tag (at the current time)
Stars: ✭ 109 (-19.85%)
Mutual labels:  capture, browser
Vue Electron Chrome
基于electron开发的应用浏览器
Stars: ✭ 132 (-2.94%)
Mutual labels:  browser
Image Promise
🎑🤞 Load one or more images, return a promise. Tiny, browser-only, no dependencies.
Stars: ✭ 129 (-5.15%)
Mutual labels:  events
Is Incognito Mode
👤Function to identify whether browser is in incognito mode 👀
Stars: ✭ 128 (-5.88%)
Mutual labels:  browser
Open Browser Github.vim
Open GitHub URL of current file, etc. from Vim editor (supported GitHub Enterprise)
Stars: ✭ 126 (-7.35%)
Mutual labels:  browser
Waterfox
The official Waterfox 💧 source code repository
Stars: ✭ 2,101 (+1444.85%)
Mutual labels:  browser
Rxmq.js
JavaScript pub/sub library based on RxJS
Stars: ✭ 134 (-1.47%)
Mutual labels:  browser
Sdk Javascript
Javascript SDK for CloudEvents
Stars: ✭ 132 (-2.94%)
Mutual labels:  events
Console Badge
🎨 Create simple badges in the browser console
Stars: ✭ 130 (-4.41%)
Mutual labels:  browser
Browser Id3 Writer
Pure JS library for writing ID3 tag to MP3 files in browsers and Node.js ✍️
Stars: ✭ 132 (-2.94%)
Mutual labels:  browser
Pokeapi Js Wrapper
PokeAPI browser wrapper, fully async with built-in cache
Stars: ✭ 129 (-5.15%)
Mutual labels:  browser
Vscode Browse Lite
🚀 An embedded browser in VS Code
Stars: ✭ 134 (-1.47%)
Mutual labels:  browser
Nocoin
No Coin is a tiny browser extension aiming to block coin miners such as Coinhive.
Stars: ✭ 1,586 (+1066.18%)
Mutual labels:  browser
Skia Wasm Port
Port of the Skia drawing library to wasm, for use in javascript (node & browser)
Stars: ✭ 131 (-3.68%)
Mutual labels:  browser
Tenfourfox
Mozilla for Power Macintosh.
Stars: ✭ 134 (-1.47%)
Mutual labels:  browser
Percy
Build frontend browser apps with Rust + WebAssembly. Supports server side rendering.
Stars: ✭ 1,856 (+1264.71%)
Mutual labels:  browser
Screenshots
A screenshot plugin for electron
Stars: ✭ 130 (-4.41%)
Mutual labels:  capture

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].