Code Monkey home page Code Monkey logo

react-native-listener's Introduction

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

react-native-listener's People

Contributors

enjoylife avatar erikras avatar rip21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-listener's Issues

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

I'm probably missing something really obvious here, but I keep getting this error.

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `StatelessComponent`.
    at invariant (invariant.js:38)
    at instantiateReactComponent (instantiateReactComponent.js:70)
    at Object.updateChildren (ReactChildReconciler.js:118)
    at ReactDOMComponent._reconcilerUpdateChildren (ReactMultiChild.js:210)
    at ReactDOMComponent._updateChildren (ReactMultiChild.js:314)
    at ReactDOMComponent.updateChildren (ReactMultiChild.js:301)
    at ReactDOMComponent._updateDOMChildren (ReactDOMComponent.js:942)
    at ReactDOMComponent.updateComponent (ReactDOMComponent.js:760)
    at ReactDOMComponent.receiveComponent (ReactDOMComponent.js:718)
    at Object.receiveComponent (ReactReconciler.js:126)

Running react 15.3.2

Loading it in which import NativeListener from 'react-native-listener';

It does load in the library successfully but when I wrap any react component with it I get this error.

ContextMenu not registering on Windows

I am using virtual box, so perhaps that might be something to do with it. But I doubt it.

onClick registers on all browsers in windows.
onContextMenu doesn't register in any browsers on windows.

It does register on my mac, and in all browsers.

Update to react 0.14

Looks like all that's needed is to use ReactDOM.findDOMNode (from react-dom) rather than getDOMNode. It runs fine as-is but warns about that.

What other event listener properties are supported?

First: thanks for the nice component! It works great when listening for onClick and onKeyDown events. However, I am having problems getting it to work with other event listeners like eg. onChange. For example:

//...

handleChange (event) {
    console.log(event)
  }

  render () {
    return (
      <div>
        <NativeListener onChange={this.handleChange.bind(this)}>
          <input type="text" />
        </NativeListener>
      </div>
    )
  }

//...

Does not log anything to the console. Am I getting something totally wrong here or are other event listeners not supported? Thanks for any advice!

Adding more events

Hi! would it be possible to add more events?
This library works perfectly well, but in my case I need to stop the propagation of a 'wheel' event.
I'd find very useful to expand the event types to fit many more situations.
Thanks for everything!

Wrapped NativeListener fails

If you have components that wrap NativeListener, the outer element will not catch events after what I think is the first rerendering. I fixed it by inserting a div between the elements. Not a huge deal, just was kinda of hard to track down. It might be related to how it creates the component in the HOC.

NativeListener doesn't compile to ES5 due to bad static var declaration

File with the fix below!
import React from 'react';
import ReactDOM from 'react-dom';
const events = [
'KeyDown',
'KeyPress',
'KeyUp',
'Click',
'ContextMenu',
'DoubleClick',
'Drag',
'DragEnd',
'DragEnter',
'DragExit',
'DragLeave',
'DragOver',
'DragStart',
'Drop',
'MouseDown',
'MouseEnter',
'MouseLeave',
'MouseMove',
'MouseOut',
'MouseOver',
'MouseUp'
];

const aliases = {
    'DoubleClick': 'dblclick'
};

const toEventName = event =>
    (aliases[event] || event).toLowerCase();

export default class NativeListener extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        const props = this.props;
        const element = ReactDOM.findDOMNode(this);
        events.forEach(event => {
            const capture = props['on' + event + 'Capture'];
            const bubble = props['on' + event];
            const stop = props['stop' + event];
            if (capture && typeof capture === 'function') {
                element.addEventListener(toEventName(event), capture, true);
            }
            if (bubble && typeof bubble === 'function') {
                element.addEventListener(toEventName(event), bubble, false);
            }
            if (stop === true) {
                element.addEventListener(toEventName(event), nativeEvent => nativeEvent.stopPropagation(), false);
            }
        });
    }

    render() {
        return this.props.children;
    }
}

NativeListener.displayName = 'NativeListener';
NativeListener.propTypes = {
    children: (props, propName) => {
        if (props[propName].length) {
            return new Error('NativeListener can only wrap one element');
        }
    },
    ...events.reduce((accumulator, event) => ({
        ...accumulator,
        [`on${event}`]: React.PropTypes.func,
        [`on${event}Capture`]: React.PropTypes.func,
        [`stop${event}`]: React.PropTypes.bool
    }), {})
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.