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-events-hooks

v1.2.2

Published

An event system to be used with react hooks

Downloads

4

Readme

react-event-hooks

An event system to be used in combination with react hooks. Decouples your components and enhances there re-usability.

Install

$ npm install react-events-hooks

Live Examples

Comming soon!!!

Usage

import React from 'react';
import { Emitter, Listener } from 'react-events-hooks';

const EVENT_NAME = 'event-fancybutton-click';

// Creates a button with an Emitter
export class FancyButton extends React.Component<{ value: number, link: string }> {
  private _emitter = Emitter.New(`${EVENT_NAME}_${this.props.link}`);
  public render = () => (
    <button onClick={this.clickHandler} >{this.props.value}</button>
  )
  private clickHandler = () => this._emitter.Notify({ value: this.props.value });
}

// Creates a label with a Listener
export class FancyLabel extends React.Component<{ link: string }> {
  public state = { number: 0 };
  private _listener!: Listener;

  public componentDidMount = () => {
    this._listener = Listener.New(`${EVENT_NAME}_${this.props.link}`, ({ value }) => {
      this.setState({ number: this.state.number + value });
    });
  }

  public componentWillUnmount = () => {
    this._listener.Destroy();
  }

  public render = () => (
    <span >{this.state.number}</span>
  )
}

Now you can create the following in a render. I've registered the last FancyButton to the first FancyLabel to illustrate the decoupling of the components.

<FancyButton value={1} link="link1" />
<FancyButton value={2} link="link1" />
<FancyLabel link="link1" />
<hr />
<FancyButton value={1} link="link2" />
<FancyButton value={2} link="link2" />
<FancyButton value={3} link="link1" />
<FancyLabel link="link2" />

Emitter class

The Emitter class sends a signal to all listeners that are registert to it.

Warning:
Don't use the new Emitter(), instead use the Emitter.New(tag: string). This is done so that the emitter is automaticly registered to the internal event system.

Static

Properties

Methods

Listener class

The Listener class executes his registered function when the emitter sends a notification.

Warning:
Don't use new Listener(), instead use the Listener.New(tag: string | string[], () => void). This is done so that the Listener is automaticly registered to the right emitter.

Static

Properties

Methods

Hooks

There are also two hooks:

  • useEmitter
  • useListener

This example is the same as above only writen with hooks.

import React, { useState } from 'react';
import { useEmitter, useListener } from 'react-events-hooks';

const EVENT_NAME = 'event-fancybutton-click';

export const FancyButton: React.FC<{ value: number, link: string }> = ({ value, link }) => {
  const notify = useEmitter(`${EVENT_NAME}_${link}`);
  const clickHandler = () => notify({ value });

  return <button onClick={clickHandler} >{value}</button>;
};

export const FancyLabel: React.FC<{ link: string }> = ({ link }) => {
  const [num, setNum] = useState(0);
  useListener(`${EVENT_NAME}_${link}`, ({ value }) => setNum((pref) => pref + value));

  return <span >{num}</span>;
};

As you can see, the listener now no longer needs to be destroyed. This is done automaticly when the component is unmounted

Changelog