react-broadcast

Reliably communicate state changes to deeply nested React elements

  • 所有者: ReactTraining/react-broadcast
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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

$ npm install --save react-broadcast

Then, use as you would anything else:

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

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

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. If you're interested in learning more about what React can do for your company, please get in touch!

主要指標

概覽
名稱與所有者ReactTraining/react-broadcast
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-09-29 04:31:12
推送於2022-11-20 03:30:26
最后一次提交2018-06-11 22:56:40
發布數27
最新版本名稱v0.7.1 (發布於 2018-06-11 22:56:40)
第一版名稱v1.0.0 (發布於 2016-09-28 21:36:11)
用户参与
星數1.3k
關注者數22
派生數58
提交數140
已啟用問題?
問題數17
打開的問題數2
拉請求數12
打開的拉請求數1
關閉的拉請求數28
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?