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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@chhoukdavy/react-postmessage

v1.0.2

Published

![NPM Version](https://img.shields.io/npm/v/@chhoukdavy/react-postmessage) ![NPM Downloads](https://img.shields.io/npm/dw/@chhoukdavy/react-postmessage) ![NPM License](https://img.shields.io/npm/l/@chhoukdavy/react-postmessage)

Downloads

18

Readme

react-postmessage

NPM Version NPM Downloads NPM License

Enable data communication between a react app and its embedded (via embed/iframe) react app.

Installation

npm:

npm install @chhoukdavy/react-postmessage

yarn:

yarn add @chhoukdavy/react-postmessage

pnpm:

pnpm add @chhoukdavy/react-postmessage

How it works

react-postmessage

Usage

Coding example

photo_2022-11-07 09 20 29

In App1:

import { useEffect, useState } from "react";
import {
  attachParamsToUrl,
  Iframe,
  initRequester,
} from "@chhoukdavy/react-postmessage";
import "./App.css";

const URL = "http://localhost:3001";
const testData = {
  test: "sth from app1",
};

type Data = {
  test: string,
};

function App() {
  const [data, setData] = useState < Data > { test: "" };
  const [show, setShow] = useState < boolean > true;

  useEffect(() => {
    initRequester <
      Data >
      {
        url: URL,
        checkOrigin: true,
        data: testData,
        hook: setData,
        close: () => setShow(false),
      };
  }, []);

  const formedURL = attachParamsToUrl(URL, [
    {
      name: "fromOrigin",
      value: "http://localhost:3000",
    },
  ]);

  return (
    <div className="App">
      <h1>App1</h1>

      <div style={{ marginTop: 20, marginBottom: 20 }}>
        Data: {JSON.stringify(data)}
      </div>

      {show && (
        <div style={{ marginTop: 20 }}>
          <Iframe url={formedURL} width={400} height={250} />
        </div>
      )}
    </div>
  );
}

export default App;

In App2:

import { useEffect, useState } from "react";
import {
  initReceiver,
  cleanUp,
  postMessage,
  signalClose,
} from "@chhoukdavy/react-postmessage";
import "./App.css";

type HookData = {
  test: string;
};

function App() {
  const [fromOrigin, setFromOrigin] = useState<string>("");
  const [data, setData] = useState<HookData>();

  useEffect(() => {
    initReceiver<HookData>({
      fromOrigin,
      setFromOrigin,
      hook: setData,
      checkOrigin: true,
    });
    return cleanUp();
  }, [fromOrigin]);

  return (
    <div className="App">
      <h1>App2</h1>
      {data && (
        <>
          <p>Data: {JSON.stringify(data)}</p>
          <div>
            <button
              style={{ marginRight: 10 }}
              onClick={() => postMessage({ test: "sth from app2" }, fromOrigin)}
            >
              Send
            </button>
            <button onClick={() => signalClose(fromOrigin)}>Close</button>
          </div>
        </>
      )}
    </div>
  );
}

export default App;

For complete coding example, please find demonstration in the example folder.

Run example

Terminal 1: Run app1 on port 3000.

cd example/app1
yarn install
yarn start

Terminal 2: Run app2 on port 3001.

cd example/app2
yarn install
yarn start

In you browser, go to localhost:3000.

API Document

Components

| Name | Type | Props | Description | | -------- | ----------- | -------------------------------------------------------------------------- | ----------------------------------------- | | Iframe | component | url, height (Optional, Default: 450), width (Optional, Default: 450) | Iframe component to load another web app. |

Utilities

| Name | Type | Props | Description | | ------------------- | ------ | ----------------------------------------------------------------------------------------- | ------------------------------------- | | initRequester | func | url, checkOrigin (Optional), data (Optional), hook (Optional), close (Optional) | Init listener in the origin app. | | initReceiver | func | fromOrigin, setFromOrigin, checkOrigin (Optional), hook | Init listener in the destination app. | | postMessage | func | data, targetOrigin | Post message back to requester. | | signalClose | func | | Signal close from destination app. | | getParam | func | href, name | Get params (name) from url. | | attachParamsToUrl | func | url, params | Attach params to url. |

TODO

  • Support for embed.
  • Add tests.