# x-channel

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

Latest version **1.1.8** (published 2023-10-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install x-channel
pnpm add x-channel
yarn add x-channel
bun add x-channel
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.8 |
| Published | 2023-10-16 |
| First published | 2023-10-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 10.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sean Freeman |
| Maintainers | theuimaster |
| Keywords | xChannel, event bus, service bus, inter component communication, react |

## Links

- npm: https://www.npmjs.com/package/x-channel
- Repository: https://github.com/thejsmaster/use-XYZ
- Homepage: https://github.com/thejsmaster/x-channel#readme
- Issues: https://github.com/thejsmaster/x-channel/issues
- npm.io page: https://npm.io/package/x-channel

## Dependencies (1)

- [react](https://npm.io/package/react.md) ^18.2.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.8 (latest) — 2023-10-16
- 1.1.7 — 2023-10-16
- 1.1.6 — 2023-10-16
- 1.1.5 — 2023-10-16
- 1.1.4 — 2023-10-16
- 1.1.3 — 2023-10-16
- 1.0.3 — 2023-10-16
- 1.0.2 — 2023-10-16
- 1.0.1 — 2023-10-16
- 1.0.0 — 2023-10-16

## README

# x-channel

A simple utility for enabling communication between components using channels in React applications.

## Installation

You can install this package via npm:

```bash
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.

```javascript
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.

```javascript
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.

```javascript
import { postMessage } from "x-channel";

postMessage("myChannel", "Hello, World!");
```

## Example

Here's a basic example of using these functions within a React component:

```javascript
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](LICENSE).

---
_Source: https://npm.io/package/x-channel · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
