# @rottitime/react-hook-message-event

> Hook for listening to window message event and posting message back to source

Latest version **1.0.10** (published 2023-01-04) · ISC license · 0 weekly downloads

## Install

```sh
npm install @rottitime/react-hook-message-event
pnpm add @rottitime/react-hook-message-event
yarn add @rottitime/react-hook-message-event
bun add @rottitime/react-hook-message-event
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.10 |
| Published | 2023-01-04 |
| First published | 2021-08-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19 |
| Author | rottitime |
| Maintainers | rottitime |
| Keywords | react, hook, message, postmessage |

## Links

- npm: https://www.npmjs.com/package/@rottitime/react-hook-message-event
- Repository: https://github.com/rottitime/react-hook-window-message-event
- Homepage: https://github.com/rottitime/react-hook-window-message-event#readme
- Issues: https://github.com/rottitime/react-hook-window-message-event/issues
- npm.io page: https://npm.io/package/@rottitime/react-hook-message-event

## 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.0.10 (latest) — 2023-01-04
- 1.0.9 — 2022-12-21
- 1.0.8 — 2021-08-10
- 1.0.7 — 2021-08-09
- 1.0.6 — 2021-08-06
- 1.0.5 — 2021-08-06
- 1.0.4 — 2021-08-06
- 1.0.3 — 2021-08-06
- 1.0.2 — 2021-08-05
- 1.0.1 — 2021-08-05
- 1.0.0 — 2021-08-05

## README

# React hook: Message event

React hook to monitor messages incoming and also allow you to post out.
Utilising and simplifing the WebAPI for [Window.postMessage()](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) and [Window: message event](https://developer.mozilla.org/en-US/docs/Web/API/Window/message_event)

## Installation

```
npm i @rottitime/react-hook-message-event
```

## Usage

The two functionalities of this this hook.

1. Listening for messages from other windows or frames
2. Send a message to parent window/iframe

Just ensure you first import your hook into your component.

```js
import useMessage from '@rottitime/react-hook-message-event'
```

### Listening for messages

The hook takes usage of the [Window: message event](https://developer.mozilla.org/en-US/docs/Web/API/Window/message_event) to allow a component to listen for messages from other windows, iframes or tabs

The hook `useMessage` takes two arguments. The first is the name of the event you want to listen to. The second is a optional callback which can be fired once a message has been recieved

The callback provides two arguments. As per the example below

1. `send` can be used to ping a message back to the source
2. `payload` gives you the data that was sent by the source

```js
const ExampleComponent = () => {
  //Listen for the message 'authenticate' and then fire a callback
  useMessage('authenticate', (send, payload) => {
    console.log('I just received : ', payload)
    //send back to sender a boolean payload
    send({ type: 'authenticate', payload: { success: true } })
  })

  return <div>Hellow world</div>
}
```

### Send a message to parent window/iframe

The hook also takes usage of the [Window.postMessage()](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) to allow you to post messages to the parent window/tab/iframe.

In the example below, we are sending the message 'Hellow world' to the parent window

```js
const ExampleComponent = () => {
  //Listen for the message 'authenticate' and then fire a callback
  const { sendToParent } = useMessage('authenticate')
  sendToParent({ type: 'authenticate', payload: { messahe: 'hello world' } })
  return <div>Hello world</div>
}
```

---
_Source: https://npm.io/package/@rottitime/react-hook-message-event · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
