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-lh

v1.2.5

Published

📣 React Loud Hailer: Publish/subscribe implementation for efficient message passing between React components.

Downloads

41

Readme

React Loud Hailer react-lh

Build Status peerDependencies Status dependencies Status npm bundle size npm

Publish/subscribe implementation for efficient message passing between React components.

Install

To install react-lh with your existing React app, run either:

$ npm install --save react-lh

or if using Yarn:

$ yarn add react-lh

Usage

Recommended: Example Todo App on JSFiddle - The Todo App example demonstrates how facilitate inter-component communications and enable decoupling between them.

The following examples uses the ECMA 9 features. If you wish to use it in CommonJS module system (i.e. in Node.js natively), you need to use require() like so:

const reactlh = require('react-lh');
const loudHailer = reactlh.loudHailer;

or concisely as:

const { loudHailer } = require('react-lh');

After installing react-lh to your React app, you need to wrap components that you wish to access the Loud Hailer API with the loudHailer() function like that:

import React, { Component } from 'react';
import loudHailer from 'react-lh';

class MyComponent extends Component {
  componentDidMount() {
    const { channel } = this.props;

    channel.on('SomeOtherActionOccurred', () => {
      // a function that executes whenever the event
      // "SomeOtherActionOccurred" is fired
    });
  }

  renderButtonClickHandler() {
    return () => {
      const { channel } = this.props;

      // fires the event "ButtonClicked"
      channel.emit('ButtonClicked');
    };
  }

  render() {
    return <button onClick={this.renderButtonClickHandler()}>Press me</button>;
  }
}

// wrap the component using Loud Hailer before exporting
export default loudHailer(MyComponent);

In the case of a function component:

import React from 'react';
import loudHailer from 'react-lh';

function FuncComponent(props) {
  const { channel } = props;

  const buttonClickHandler = () => {
    channel.emit('ButtonClicked');
  };

  return <button onClick={buttonClickHandler}>Press me</button>;
}

// wrap the component using Loud Hailer before exporting
export default loudHailer(FuncComponent);

By default, the Loud Hailer API will be accessible through the channel props property. The property name can be changed by passing an option to Loud Hailer wrapper's second options argument like this:

import React from 'react';
import loudHailer from 'react-lh';

function FuncComponent(props) {
  // notice that "pipe" is used here
  const { pipe } = props;

  const buttonClickHandler = () => {
    pipe.emit('ButtonClicked');
  };

  // ...
}

const options = {
  property: 'pipe',
};
export default loudHailer(FuncComponent, options);

You can even further simplify your component as such:

import React from 'react';
import loudHailer from 'react-lh';

export default loudHailer(({ channel }) => {
  const buttonClickHandler = () => {
    channel.emit('ButtonClicked');
  };

  return <button onClick={buttonClickHandler}>Press me</button>;
});

React Hooks

With the introduction of hooks in React 16.8 or later, you can use react-lh's React Hooks:

  • useChannel
  • useLoudHailer
  • useOnEvent
  • useOnceEvent
import React, { useState } from 'react';
import { useLoudHailer } from 'react-lh';

export default (props) => {
  const [value, setValue] = useState('none');

  useLoudHailer((channel) => {
    channel.on('valueGiven', (newValue) => {
      setValue(newValue);
    });
  }, []);

  return <span>{`The value given was: ${value}`}</span>;
};

Or more concisely using useOnEvent:

import React, { useState } from 'react';
import { useOnEvent } from 'react-lh';

export default (props) => {
  const [value, setValue] = useState('none');

  useOnEvent(
    'valueGiven',
    (newValue) => {
      setValue(newValue);
    },
    []
  );

  return <span>{`The value given was: ${value}`}</span>;
};

Cross-Window Event Propagation

It is possible for events emitted by components on one window to be propagated to another window through React Loud Hailer. This is made possible by browsers' localStorage API implementation. To enable this feature, you need to wrap your top-most component (e.g. App) with the Loud Hailer CrossWindow component like this:

import React from 'react';
import ReactDOM from 'react-dom';
import { CrossWindow } from 'react-lh';

import App from './App';

const channels = ['default'];

ReactDOM.render(
  <CrossWindow channels={channels}>
    <App />
  </CrossWindow>,
  document.getElementById('root')
);

The channels property indicate which channels can be communicated across all the open windows of your website. The default channel namespace is 'default'.