Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/luqmanoop/react-mitt

React event emitter / pubsub 👉🏻👈🏽
https://github.com/luqmanoop/react-mitt

eventemitter events mitt pubsub react typescript

Last synced: 3 months ago
JSON representation

React event emitter / pubsub 👉🏻👈🏽

Awesome Lists containing this project

README

        

# react-mitt

> React event emitter / pubsub


react-mitt build status
react-mitt code coverage

A react wrapper for the awesome [mitt](https://github.com/developit/mitt) library

Live demo https://codesandbox.io/s/react-mitt-demo-n11ii

## Install

```sh
$ npm install react-mitt
```

## Usage

Wrap your app in the `MittProvider` component, providing descendants access to the `useMitt` hook for event pubsub

```jsx
import { MittProvider } from "react-mitt"

function App() {
return (




)
}
```

## Example

Once you wrap your app with the `MittProvider` as demonstrated [above](https://github.com/codeshifu/react-mitt#usage), event pubsub becomes a breeze with the `useMitt` hook

```jsx
import { useMitt } from 'react-mitt'

function ComponentA() {
const { emitter } = useMitt()

const handleClick = () => {
emitter.emit('foo', { data: 'ComponentA says foo!'})
}

return emit!
}

function ComponentB() {
const { emitter } = useMitt()

useEffect(() => {
// listen and respond to 'foo' events
emitter.on('foo', event => alert(event.data))
}, [])

return ...
}

```

## Hook

The `useMitt` hook has the following signature

```js
const { emitter } = useMitt()
```

For usage of the **emitter** object, see **mitt** API [docs](https://github.com/developit/mitt#api)

## License
[MIT License](https://github.com/codeshifu/react-mitt/blob/master/license)