npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

joy-react-broadcast

v0.6.9

Published

Reliably communicate state changes to deeply nested React elements

Downloads

12

Readme

react-broadcast Travis npm 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.

It was originally built to solve issues that arose from using react-router together with 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, 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

Using yarn:

$ yarn add react-broadcast

Then, use as you would anything else:

// using ES6 modules
import { Broadcast, Subscriber } from 'react-broadcast'

// using CommonJS modules
var Broadcast = require('react-broadcast').Broadcast
var Subscriber = require('react-broadcast').Subscriber

The UMD build is also available on unpkg:

<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 totally contrived example, but illustrates the basic functionality we're after:

import React from 'react'
import { Broadcast, Subscriber } from 'react-broadcast'

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

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: users[0]
  }

  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 (
      <Broadcast channel="currentUser" value={this.state.currentUser}>
        <UpdateBlocker>
          <Subscriber channel="currentUser">
            {currentUser => <p>The current user is {currentUser.name}</p>}
          </Subscriber>
        </UpdateBlocker>
      </Broadcast>
    )
  }
}

You may prefer to wrap these components into channel-specific pairs to avoid typos and other problems with the indirection involved with the channel strings:

// Broadcasts.js
import { Broadcast, Subscriber } from 'react-broadcast'

const CurrentUserChannel = 'currentUser'

export const CurrentUserBroadcast = (props) =>
  <Broadcast {...props} channel={CurrentUserChannel}/>

export const CurrentUserSubscriber = (props) =>
  <Subscriber {...props} channel={CurrentUserChannel}/>

// App.js
import { CurrentUserBroadcast, CurrentUserSubscriber } from './Broadcasts'

<CurrentUserBroadcast value={user}/>
<CurrentUserSubscriber>{user => ...}</CurrentUserSubscriber>

Enjoy!

About

react-broadcast is developed and maintained by React Training. If you're interested in learning more about what React can do for your company, please get in touch!