An open API service indexing awesome lists of open source software.

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

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