# use-publish-subscribe

> Hook that can be used to share some state without extracting of the state into parent

Latest version **0.0.6** (published 2022-01-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install use-publish-subscribe
pnpm add use-publish-subscribe
yarn add use-publish-subscribe
bun add use-publish-subscribe
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2022-01-25 |
| First published | 2020-10-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | andrewbeletskiy |
| Maintainers | andrewbeletskiy |
| Keywords | react, hook, state, publisher, subscriber |

## Links

- npm: https://www.npmjs.com/package/use-publish-subscribe
- Repository: https://github.com/whiteand/use-publish-subscribe
- Homepage: https://github.com/whiteand/use-publish-subscribe#readme
- Issues: https://github.com/whiteand/use-publish-subscribe/issues
- npm.io page: https://npm.io/package/use-publish-subscribe

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.8.0
- [@types/react](https://npm.io/package/@types/react.md) ^16.9.53

## 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

- 0.0.6 (latest) — 2022-01-25
- 0.0.5 — 2022-01-25
- 0.0.4 — 2022-01-25
- 0.0.2 — 2020-10-18

## README

# use-publish-subscribe

[![Build Status](https://travis-ci.com/whiteand/use-publish-subscribe.svg?branch=master)](https://travis-ci.com/whiteand/use-publish-subscribe)
[![Coverage Status](https://coveralls.io/repos/github/whiteand/use-publish-subscribe/badge.svg?branch=master)](https://coveralls.io/github/whiteand/use-publish-subscribe?branch=master)

**Size: 3.39 KB** (minified and gzipped). Only React dependency. [Size Limit](https://github.com/ai/size-limit) controls the size.

# Where can I use it?

The main case is when you need to share some data between 2+ child components, but without rerendering of the parent component.

For example:

```javascript
import { usePublishSubscribe } from 'use-publish-subscribe'
import { Renderer } from './Renderer.js'
import { Counter } from './Counter.js'

function Parent() {
  const [publish, subscribe] = usePublishSubscribe(0)
  return (
    <>
      <Counter onChange={publish}>
      <Renderer subscribe={subscribe}>
    </>
  )
}
```
```javascript
// Counter.js
function Counter({ onChange }) {
  const counter = useRef(0)

  const increase = useCallback(() => {
    counter.current += 1;
    onChange(counter.current)
  }, [counter, publish])

  return <button onClick={increase}>Increase</button>
}
```

```javascript
// Renderer.js
function Renderer({ subscribe }) {
  const [counter, setCounter] = useState(0)

  useEffect(() => subscribe(setCounter, true), [setCounter, subscribe])

  return <div>Counter: {counter}</div>
}
```

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