• Stars
    star
    135
  • Rank 261,295 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

<Subscribe> component to automatically consume observables declaratively in React JSX

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

More Repositories

1

git-blame-someone-else

Blame someone else for your bad code.
Shell
10,058
star
2

core-decorators

Library of stage-0 JavaScript decorators (aka ES2016/ES7 decorators but not accurate) inspired by languages that come with built-ins like @​override, @​deprecate, @​autobind, @​mixin and more. Popular with React/Angular, but is framework agnostic.
JavaScript
4,501
star
3

nofux

Unpredictable state container for JavaScript apps.
TypeScript
308
star
4

multiline-template

Multiline tagged templates using a pipe |, followed by a space, to signal line start, no more crazy indent hacks.
JavaScript
55
star
5

ember-string-interpolate

Adds string interpolation as a computed property to Ember.js (i.e. no more unreadable getter concatenation)
JavaScript
46
star
6

sweet-async-await

Sweet.js macros to support the async/await ES7 proposed feature
JavaScript
36
star
7

web-retina-emulator

A tiny script for testing what your website looks like on a 2x pixel density (retina) display.
JavaScript
30
star
8

broccoli-babel-boilerplate

[NO LONGER MAINTAINED] A boilerplate, using Broccoli, to author JavaScript libraries in ES6/ES7++ but transpile and distribute using Babel.js
JavaScript
28
star
9

ember-computed-injection

Inject anything from your Ember container into any class as a property. (i.e. sort of like `needs` for everything else)
JavaScript
22
star
10

loading-attribute-eagle-polyfill

loading="eagle" attribute polyfill
21
star
11

wasm-anyref-demo

Playing around with WebAssembly (Wasm) anyref using JavaScript's GC as the allocator
WebAssembly
21
star
12

webpack-rxjs-externals

Generate all the RxJS v5 "externals" for your webpack config.
JavaScript
17
star
13

ember-computed-content-controller

A computed property for injecting a unique instance of a content controller (ObjectController, ArrayController, etc) into an Ember class instance. Useful for nested ArrayControllers, among other things.
JavaScript
14
star
14

jayscript

A very small subset dialect of JavaScript
JavaScript
11
star
15

npm-publish-scoped

Publish any package temporarily to your @user scope, while you wait for them cut a release. Useful when your PR is merged and you don't want to wait.
JavaScript
10
star
16

resume

Jay's resume, hand-written in WebAssembly text
WebAssembly
9
star
17

redux-transaction

JavaScript
8
star
18

ember-model-batch

Batch multiple model requests into a single ajax call (ember-data or ember-model)
JavaScript
8
star
19

rxjs-compatibility

Use RxJS v5 with v4 and vice versa
JavaScript
7
star
20

react-map-props

Transform component props before they're passed to your component
JavaScript
7
star
21

stackblur

A fast almost Gaussian Blur For Canvas by Mario Klingemann (bower mirror)
6
star
22

shady-dom

Shady under a tree, not sketchy.
6
star
23

react-webpack-boilerplate

NOTE: As of now, this is used mostly internally by our team so it'll have some things like Semantic UI, etc that you might not want.
JavaScript
6
star
24

ember-computed-smart-property

Ember Computed Properties without needing to provide dependent keys (it figures it out for you)
JavaScript
5
star
25

wazzup

WebAssembly codegen, inspired by Binaryen. NOT intended for use, it was just created to learn the spec better.
TypeScript
5
star
26

uikit-cpp-win32

Win32 C++ Application Framework inspired by UIKit
C++
4
star
27

wasmjs-binary-bundles

Experimenting with combining WebAssembly and JavaScript into a single file bundle
JavaScript
4
star
28

using-llvm-from-rust-to-generate-webassembly

Code for my Medium article "Using LLVM from Rust, to generate WebAssembly"
Rust
4
star
29

github-clone-pr

Clone a github PR easily using just the github.com PR URL
JavaScript
4
star
30

ember-relay

That whole `Promises` thing without even paying attention.
JavaScript
4
star
31

react-redux-transaction

JavaScript
3
star
32

shortcut.js

Add keyboard shortcuts that simulate an element being clicked
JavaScript
3
star
33

ember-data-json-patch-adapter

Adds support for JSON Patch (RFC 6902) updates
3
star
34

rxjs-babel-only-tree-shaking-example

JavaScript
3
star
35

stutter.js

JavaScript Preprocessor Library
JavaScript
2
star
36

string.interpolate.js

A generic string interpolation helper for JavaScript that is library agnostic.
JavaScript
2
star
37

talks

A list of the talks I can/will/have given
2
star
38

benlesh

JavaScript
2
star
39

core-decorators-artifacts

Built files from https://github.com/jayphelps/core-decorators.js
JavaScript
2
star
40

react-router-query-params

Better query params management for react-router users
1
star
41

g-example

JavaScript
1
star
42

wasm-cdn

1
star
43

nails

Node.js web application framework based on JavaScript ES6/ES7 features
1
star
44

graffiti

JavaScript
1
star
45

jayphelps

1
star
46

shadow-dom.css

a CSS reset class name you can add to emulate/polyfill being inside a Shadow DOM from the preventing outside styles from bleeding in.
CSS
1
star
47

stunning-palm-tree

EJS
1
star
48

graffiti-runtime

JavaScript
1
star
49

gatsby-dev-ssr-issue

JavaScript
1
star
50

cool-story-bro-theme

A minimalistic blue-steel colored theme for Apple Keynote that uses Bebas Neue.
1
star
51

ember-media

Ember-idiomatic views for <video> and <audio>
JavaScript
1
star
52

jest-event-demo

JavaScript
1
star