https://github.com/kirill-dev-pro/mebus-react
React hook to use MeBus
https://github.com/kirill-dev-pro/mebus-react
message-bus react type-safe validation
Last synced: 3 months ago
JSON representation
React hook to use MeBus
- Host: GitHub
- URL: https://github.com/kirill-dev-pro/mebus-react
- Owner: kirill-dev-pro
- Created: 2024-03-01T08:48:38.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-03-04T16:35:45.000Z (over 1 year ago)
- Last Synced: 2025-04-11T01:49:33.460Z (3 months ago)
- Topics: message-bus, react, type-safe, validation
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/mebus-react
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MeBus-React
## Description
React hook for MeBus. MeBus is a type-safe wrapper around the browser's native `MessageEvent` API.
This package has 3 dependencies:
- `io-ts` for event schema validation
- `mebus` for the core functionality
- `react` for the react hook## Installation
```bash
npm install mebus-react
```## Example
```jsx
import { useMeBus } from 'mebus-react';
import { useState } from "react";
import * as t from "io-ts";const myEventSchema = {
increaseCounter: t.type({ number: t.number }),
};const Counter = () => {
const [count, setCount] = useState(0);useMeBus({
eventSchema: myEventSchema,
eventCallbacks: {
increaseCounter: (payload) => {
setCount((count) => count + payload.number);
},
},
});return
count is {count};
};function App() {
const publish = useMeBus({ eventSchema: myEventSchema });return (
publish("increaseCounter", { number: 1 })}>
);
}
```## API
### `useMeBus`
```tsx
function useMeBus(options: {
eventSchema: T;
eventCallbacks?: MeBusEventCallbacks;
}): MeBusPublish;
```- `eventSchema`: The event schema to use for the MeBus instance.
- `eventCallbacks`: An object with event names as keys and event handlers as values. The event handlers are called when a message is received with the corresponding event name.
- Returns a `publish` function that can be used to send messages to other MeBus instances.
## License
MIT