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-message-event

v1.0.5

Published

React hook used to communicate between components using the native `message` event listener

Downloads

114

Readme

react-message-event

react-message-event is a React hook that allows communication between components using the native message event listener. This hook can be used to send and receive messages with specific keys and payloads. This library uses the postMessage function to communicate between components, which is only possible between components that are in the same window or iframe. With this library, you can easily send messages between sibling and parent-child components, without the need to pass props down through multiple levels of components.

Installation

npm i react-message-event

Usage

useMessage<T = any, P = any>(keys: string[], callback: (message: Message<T, P>) => void): void

The useMessage hook listens for messages with the specified keys and calls the provided callback with the received message. The message is of the Message<T, P> type, where T is the type of the key and P is the type of the payload.

sendMessage<T = any, P = any>(message: Message<T, P>): void

The sendMessage function sends a message to any component that is listening for messages with the same key as the sent message. The message is of the Message<T, P> type, where T is the type of the key and P is the type of the payload.

Example

In this example, when the button in ComponentB is clicked, it sends a message with the key set-id and a payload of 1. ComponentA is listening for messages with the key set-id and will receive the message and call the provided callback with the received message.

import React from 'react';
import { useMessage, sendMessage } from 'react-message-event';

const ComponentA: FC = () => {
	useMessage<string, number>(['set-id'], (message) => {
		console.log(message); // { key: 'set-id', payload: 1 }
	});

	return <div />;
};

const ComponentB: FC = () => {
	const handleClick = () => sendMessage({ key: 'set-id', payload: 1 });

	return (
		<div>
			<button onClick={handleClick}>Send Message</button>
		</div>
	);
};

Note

This library uses the native message event listener and the postMessage function to communicate between components. This communication is only possible between components that are in the same window or iframe.