Skip to content

Imperative API for installing DOM event handlers #284

Closed
@andreypopp

Description

@andreypopp

Currently React allows installing event handler on DOM events only in declarative fashion, via component attributes at the time of a component instantiation.

I think this is nice and allows automatically uninstall such handlers in case of a component disposal. But for implementing some "low-level" machinery I think it would be useful to allow handlers to be installed after the component instantiation at arbitrary moments. That would allow installing an event handler in a mixin's componentDidMount (with a corresponding uninstall of such event handler in componentWillUnmount) callback.

For example currently I use jQuery for trapping clicks on anchors and invoking HTML5 pushState API instead of letting a browser to reload a page:

  ...
  componentDidMount: function() {
    $(this.getDOMNode()).on('click.react-route', 'a', function(e) {
      e.preventDefault();
      // invoke HTML5 pushState API
    });
  },

  componentWillUnmount: function() {
    $(this.getDOMNode()).off('click.react-route');
  }
 ...

The problem with that is I'm not able to use goodies provided by React's event plugins. So what I want instead is to handle click events with React's event system and install such handler in componentDidMount callback so I can extract such functionality into a mixin. Something like:

  ...
  handleClick: function(e) {
    // check if event's target is an anchor and invoke pushState API
  },

  componentDidMount: function() {
    this.addEventListener(eventTypes.onClick, this.handleClick);
  },

  componentWillUnmount: function() {
    this.removeEventListener(eventTypes.onClick, this.handleClick);
  }
 ...

Though maybe addEventListener and removeEventListener names are to verbose...

Other examples include handling focus/blur and arrow key clicks to control focus inside a component, ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions