New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

x-channel

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

x-channel

React library for parent to child and child to parent communication over channels to share state/data or function.

latest
Source
npmnpm
Version
1.1.8
Version published
Maintainers
1
Created
Source

x-channel

A simple utility for enabling communication between components using channels in React applications.

Installation

You can install this package via npm:

npm install x-channel

Usage

useXChannel

The useXChannel hook provides a way to listen for messages on a specific channel. When a message is sent to that channel, the provided callback function will be invoked.

import { useXChannel } from "x-channel";

useXChannel("myChannel", (message) => {
  console.log("Received a message:", message);
});

getListenerCount

The getListenerCount function allows you to check how many listeners are currently registered for a specific channel.

import { getListenerCount } from "x-channel";

const count = getListenerCount("myChannel");
console.log("Number of listeners for myChannel:", count);

postMessage

The postMessage function allows you to send a message to a specific channel. It will notify all registered listeners for that channel.

import { postMessage } from "x-channel";

postMessage("myChannel", "Hello, World!");

Example

Here's a basic example of using these functions within a React component:

import React from "react";
import { postMessage, useXChannel } from "x-channel";

function App() {
  const post = useXChannel("myChannel", (message) => {
    console.log("Received a message from Test:", message);
  });

  const sendMessage = () => {
    post("myChannel", "Hello, from App!");
  };

  return (
    <div>
      <button onClick={sendMessage}>Send Message</button>
      <Test />
    </div>
  );
}
function Test() {
  const post = useXChannel("myChannel", (message) => {
    console.log("Received a message from APP:", message);
  });

  const sendMessage = () => {
    post("myChannel", "Hello, from Test!");
  };

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

License

This package is released under the MIT License.

Keywords

xChannel

FAQs

Package last updated on 16 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts