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

react-observable-subscribe

v0.1.7

Published

<Subscribe> component to automatically consume observables declaratively in JSX

Downloads

39

Readme

react-observable-subscribe

Subscribe to an Observable declaratively with the <Subscribe>{observable}</Subscribe> component.

Install

npm install --save react-observable-subscribe

Usage

This library's default export is a Subscribe component which you can use in your JSX to declaratively subscribe and render Observable streams. Just pass the observable as the only child to the the component.

You can apply any operators you want to your observable before passing it to <Subscribe>--you don't have to use RxJS v5.0, the only requirement is the the observable supports observable[Symbol.observable]() from the proposed Observable spec.

This library doesn't come with any actual Observable implementation itself.

import Subscribe from 'react-observable-subscribe';
// ...other imports

class Example extends Component {
  render() {
    return (
      <div>
        <div>
          <h3>Every 100ms:</h3>
          <div>
            <Subscribe>{this.props.stream}</Subscribe>
          </div>
        </div>
        <div>
          <h3>Every 1s:</h3>
          <div>
            <Subscribe>{this.props.stream.throttleTime(1000)}</Subscribe>
          </div>
        </div>
        <div>
          <h3>Every 100ms w/ &lt;input&gt; element: </h3>
          <div>
            <Subscribe>
              {this.props.stream.map(
                value => <input value={value} readOnly />
              )}
            </Subscribe>
          </div>
        </div>
      </div>
    );
  }
}

// This Observable will emit an incrementing
// number every 100ms
let stream = Observable.interval(100);
ReactDOM.render(<Example stream={stream} />, container);

ezgif-79206338

The observable can emit simple primitives (e.g. strings, numbers) or you can even emit JSX elements! Each "onNext" just needs to be a single value, arrays are not supported because of React limitations.

When the observable emits new values, only the content inside <Subscribe> will re-render, not the component in which you declare it, so it's very efficient.

Depending on your preferences, you might find it helpful to use a shorter name for Subscribe when you import it. Since it's the default export, what you name it is totally up to you:

import Sub from 'react-observable-subscribe';

// etc

<div>
  <Sub>{stream.throttleTime(1000)}</Sub>
</div>

Server-side rendering

If you do Server-side rendering with React.renderToString, it's important to note that since React doesn't support asynchronous rendering <Subscribe> will subscribe() to the stream but then immediately unsubscribe(), so any synchronously emitted value will be rendered, otherwise nothing. One approach to emit a synchronous value in RxJS v5 is the startWith(value) operator, e.g. you might emit some "Loading..." text or <img src="spinner.gif />.

Observable.ajax('/some/resource/that/will/render/asynchronously')
  .map(event => event.response) // event == { response: 'async value', ... }
  .startWith('initial value rendered synchronously')

Learn more about Schedulers in RxJS