Skip to content

Dispatch synthetic events and define custom ones #7029

@FezVrasta

Description

@FezVrasta

Right now you can only dispatch native events to the DOM nodes, but there's not any way to dispatch synthetic events on the Virtual DOM nodes.

I'd like to be able to dispatch synthetic events, in addition, I'd like a way to define custom event listeners.

Usecase

For example, I've a component decorator that adds a resize event on a DOM element when it get resized. I'd like to be able to dispatch the resize event to such element and be able to listen to it using onResize.

Example:

function DecoratedComponent(props) {
  return <div onResize={evt => console.log('resized')}>foobar</div>;
}
resizeAware(DecoratedComponent);

For reference, this is the logic of my decorator that would dispatch the event.
https://gist.github.com/FezVrasta/0324991bbdf044fd2723eebbc344adff

As you see, right now I'm using findDOMNode(this.componentNode).dispatchEvent(new Event('resize')) which makes the resize event available only to the real DOM node.
This means that I must listen for the event in this way:

class DecoratedComponent extends React.Component {
  componentDidMount() {
    findDOMNode(this).addEventListener('resize', evt => console.log('resized'));
  }
  render() {
    return <div>foobar</div>;
  }
}
resizeAware(DecoratedComponent);

This is very inconvenient and not flexible. We'd need a way to dispatch synthetic events and be able to listen to them using the onSomething properties.

I think it would be a great addition to React.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions