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

friendlyfire

v1.0.0

Published

Simple pubsub/event system for React component communication

Downloads

1

Readme

FriendlyFire

Easy cross-component communication using pubsub, designed for React. No dependencies.

Why?

React provides a built-in mechanism for parent components to communicate with child components: props. In order to allow components to communicate with parent, sibling, or unrelated components, the are various solutions; most famously Flux. While it's a great solution, especially for large and complex projects, it requires a lot of boilerplate code, and spreads logic across many files. Perhaps most annoying of all, it requires parent components to send callback handlers to children, sometimes over many generations.

Using FriendlyFire, components easily subscribe to events triggered by other components. Components that trigger events don't need to know what will consume them, they don't need to add extra props for callbacks.

Just by looking at a component's subscription methods, you can easily tell which events, emitted by a particular component, it subscribes to. The emitting components on the other hand, need not care who subscribes.

This model of communication enables modularity/portability of components, reducing the dependencies of components on each other, making component dependency exist in only one direction, from parent to child.

Example

import ff from 'friendlyfire';

class ModalShower extends React.Component {
  constructor() {
    super();
    this.state = { modalOpen: false };
  }

  componentDidMount() {
    ff.init(this); // Register with FriendlyFire to have subscribers registered
  }

  // Auto-subscribes to 'close' event from Modal components
  onModalClose() {
    this.setState({ modalOpen: false });
  }

  render() {
    return(
      <div>
        <button onClick={this.openModal.bind(this)}>Open Modal</button>
        <Modal open={this.state.modalOpen}>
          Contents of modal
        </Modal>
      </div>
    );
  }
}

class Modal extends React.Component {
  componentDidMount() {
    ff.init(this); // Register with FriendlyFire to make trigger(…) method available
  }

  handleOuterClick(e) {
    if (e.target === e.currentTarget) {
      this.trigger('close');
    }
  }

  render() {
    return (
      <div className='outerModal'
        onClick={this.handleOuterClick.bind(this)}
      >
        <div className='innerModal'>
          <div className='closeButton' onClick={this.handleOuterClick.bind(this)}>
            {this.props.children}
          </div>
        </div>
      </div>
    )
  }
}

API

ff.init(component)

Initializes a react component to be able to publish and subscribe using FriendlyFire.

Recommended that you put this into componentDidMount and pass in this as the parameter.

Add trigger and will scan the components' methods looking potential subscribers, and use them.

.trigger(eventName)

Will fire any subscribers listening for the specified event from the triggering component.

.onComponentNameEventname(data)

To register a subscriber on a component, create a method on it using the above pattern.

Note: The event specified needs to be all lowercase (with the exception of the first character).