npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-event-emitter

v0.2.1

Published

DOM event emitter for React

Downloads

16

Readme

Property Setter logo

react-event-emitter

DOM CustomEvent emitter written in React. It's meant to be used at any level below the target element in the DOM tree.

Why?

Interacting between React and other libraries can sometimes be a bit cumbersome because of the difference in paradigms. Maybe in the future React will allow to work in an easier way with CustomEvents, but for now you'd need to switch from the usual declarative logic in writting components in JSX to an imperative way to hook up the logic in React and your other library's logic.

It was originally made to interact with CustomElements inside of a React app.

Requirements, installation and usage

You need to have a recent version Node.js installed with a working npm command.

Also, this has react and react-dom as peer dependencies.

To install, run in a shell:

npm install react-event-emitter

Then, in the JavaScript you want to use it, import it as a ES module:

import EventEmitter from 'react-event-emitter';

Or, if you still use commonjs modules for browser code, import it as such:

const EventEmitter = require('react-event-emitter');

To see more, refer to the examples section.

API

props

| name | type | required | default | | ---------- | --------- | -------- | ----------- | | children | ReactNode | no | null | | eventType | string | yes | undefined | | detail | any | no | undefined | | waitUntil | Promise | no | undefined | | bubbles | boolean | no | undefined | | cancelable | boolean | no | undefined | | composed | boolean | no | undefined |

notes

  1. To avoid race-conditions, when an event is sent before the element that is supposed to listen to it does so, a waitUntil prop has been introduced.

  2. bubbles, cancelable, and composed are directly sent as-is to the CustomEvent constructor, see here for more information.

Example usage

simple

Some DOM element higher up the tree is listening to a load event containing some data

import EventEmitter from 'react-event-emitter';

const Component = ({ data }) => <EventEmitter eventType="load" detail={data} />;

with Custom Elements

In the case where the definition for data-visualisation might not be ready yet (code-splitting and async loading of the definition), but it's the element that is supposed to listen for the emitted event, use the waitUntil prop to wait for its definition to be loaded.

import EventEmitter from 'react-event-emitter';

const Component = ({ data }) => (
  <data-visualisation>
    <EventEmitter
      eventType="load"
      detail={data}
      waitUntil={customElements.whenDefined('data-visualisation')}
    />
  </data-visualisation>
);

Development

Library written in TypeScript.

You can run the tests (unit, linting, and type-checking) by running npm run test.

To build a new bundle, run npm run build, or npm run build:dev for development mode.

Acknowledgement

This package's code was started while working within the Molecular Modeling and Bioinformatics (MMB) group at the Institute for Research in Biomedicine (IRB).