# react-broadcast

> Reliably communicate state changes to deeply nested React elements

Latest version **0.7.1** (published 2018-06-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-broadcast
pnpm add react-broadcast
yarn add react-broadcast
bun add react-broadcast
```

## Health

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

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.7.1 |
| Published | 2018-06-12 |
| First published | 2016-09-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/react-broadcast) |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 83.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1312 |
| Author | Michael Jackson |
| Maintainers | mjackson |
| Keywords | react, context, broadcast, publish, subscribe, subscriber, subscription, subscriptions |

## Links

- npm: https://www.npmjs.com/package/react-broadcast
- Repository: https://github.com/ReactTraining/react-broadcast
- Homepage: https://github.com/ReactTraining/react-broadcast#readme
- Issues: https://github.com/ReactTraining/react-broadcast/issues
- npm.io page: https://npm.io/package/react-broadcast

## Dependencies (2)

- [warning](https://npm.io/package/warning.md) ^3.0.0
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.0

## 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.7.1 (latest) — 2018-06-12
- 0.7.0-rc.4 (next) — 2018-02-08
- 0.7.0 — 2018-03-30
- 0.6.2 — 2018-02-01
- 0.6.2-rc.1 — 2018-02-01
- 0.7.0-rc.3 — 2018-01-30
- 0.7.0-rc.2 — 2018-01-18
- 0.7.0-rc.1 — 2018-01-18
- 0.6.1 — 2017-12-18
- 0.6.0 — 2017-11-29
- 0.5.3 — 2017-11-29
- 0.5.2 — 2017-09-12
- 0.5.1 — 2017-09-12
- 0.5.0 — 2017-09-12
- 0.4.2 — 2017-09-08
- … 11 more at https://npm.io/package/react-broadcast/versions

## README

# react-broadcast [![Travis][build-badge]][build] [![npm package][npm-badge]][npm]

[build-badge]: https://img.shields.io/travis/ReactTraining/react-broadcast/master.svg?style=flat-square
[build]: https://travis-ci.org/ReactTraining/react-broadcast
[npm-badge]: https://img.shields.io/npm/v/react-broadcast.svg?style=flat-square
[npm]: https://www.npmjs.com/package/react-broadcast

[`react-broadcast`](https://www.npmjs.com/package/react-broadcast) provides a reliable way for React components to propagate state changes to their descendants deep in the component hierarchy, bypassing intermediaries who `return false` from [`shouldComponentUpdate`](https://reactjs.org/docs/react-component.html#shouldcomponentupdate).

It was originally built to solve issues that arose from using [`react-router`](https://www.npmjs.com/package/react-router) together with [`react-redux`](https://www.npmjs.com/package/react-redux). The router needed a safe way to communicate state changes to `<Link>`s deep in the component hierarchy, but `react-redux` relies on `shouldComponentUpdate` for performance. `react-broadcast` allows the router to work seamlessly with Redux and any other component that uses `shouldComponentUpdate`.

**Please note:** As with anything that uses [context](https://reactjs.org/docs/context.html), this library is experimental. It may cease working in some future version of React. For now, it's a practical workaround for the router. If we discover some better way to do things in the future, rest assured we'll do our best to share what we learn.

## Installation

    $ npm install --save react-broadcast

Then, use as you would anything else:

```js
// using ES6 modules
import { createContext } from "react-broadcast";

// using CommonJS modules
var createContext = require("react-broadcast").createContext;
```

The UMD build is also available on [unpkg](https://unpkg.com):

```html
<script src="https://unpkg.com/react-broadcast/umd/react-broadcast.min.js"></script>
```

You can find the library on `window.ReactBroadcast`.

## Usage

The following is a contrived example, but illustrates the basic functionality we're after:

```js
import React from "react";
import { createContext } from "react-broadcast";

const users = [{ name: "Michael Jackson" }, { name: "Ryan Florence" }];

const { Provider, Consumer } = createContext(users[0]);

class UpdateBlocker extends React.Component {
  shouldComponentUpdate() {
    // This is how you indicate to React's reconciler that you don't
    // need to be updated. It's a great way to boost performance when
    // you're sure (based on your props and state) that your render
    // output will not change, but it makes it difficult for libraries
    // to communicate changes down the hierarchy that you don't really
    // know anything about.
    return false;
  }

  render() {
    return this.props.children;
  }
}

class App extends React.Component {
  state = {
    currentUser: Provider.defaultValue
  };

  componentDidMount() {
    // Randomly change the current user every 2 seconds.
    setInterval(() => {
      const index = Math.floor(Math.random() * users.length);
      this.setState({ currentUser: users[index] });
    }, 2000);
  }

  render() {
    return (
      <Provider value={this.state.currentUser}>
        <UpdateBlocker>
          <Consumer>
            {currentUser => <p>The current user is {currentUser.name}</p>}
          </Consumer>
        </UpdateBlocker>
      </Provider>
    );
  }
}
```

Enjoy!

## About

react-broadcast is developed and maintained by [React Training](https://reacttraining.com). If you're interested in learning more about what React can do for your company, please [get in touch](mailto:hello@reacttraining.com)!

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