Git Product home page Git Product logo

react-broadcast's Introduction

react-broadcast Travis npm package

This library is considered experimental and is probably not going to be developed further. Subscriptions take you outside of the React model, and make it harder for you to predict when elements will re-render

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.

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() {
    // We can get around the blocker using a <Subscriber>
    return (
      <Subscriber channel="currentUser">
        {currentUser => <p>The current user is {currentUser.name}</p>}
      </Subscriber>
    )
  }
}

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

That's it! Enjoy :)

react-broadcast's People

Contributors

greenkeeperio-bot avatar mjackson avatar ryanflorence avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.