question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Alternative to Observe for Sideways Data Loading

See original GitHub issue

For reference. We’re exploring an alternative to the observe function proposed in #3398.

Instead of having a separate function that defines a list of dependencies on Observables, we could allow the render function itself trigger a bunch of side-effects that registers the currently executing component with some Observables.

render() {
  var data = request(this.props.url);
  if (data === null) return <div>Loading...</div>;
  return <div>{data}</div>;
}

Side-effects! 😲

The implementation of request wouldn’t return an Observable. Instead it would tell React that the currently executing component has a dependency on this Observable and needs to rerender anytime the subscription fires. It returns the last value received by this particular Observable… somehow.

function request(url) {
  return React.readAndSubscribe(Observable.fetch(url));
}

If an Observable is no longer subscribed to in the next render pass, it is automatically unsubscribed. Perhaps the Observable API is not ideal for this model.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:38 (23 by maintainers)

github_iconTop GitHub Comments

4reactions
benleshcommented, Apr 30, 2016

@sebmarkbage was showing @jayphelps this, and I think I mentioned it in short-form on Twitter… but providing this functionality would enable people to do something like the following (typical autocomplete example):

import { Component } from 'react';
import { boundSubject } from 'bound-subject-decorator';
import * as Rx from 'rxjs';

export default MyComponent extends Component {
  @boundSubject // binds the subject's handlers to the subject instance
  keypresses = new Subject();

  observe() {
    return {
      // compose keypresses into a throttle stream of async auto complete data
      autoCompleteData: this.keypresses
        .map((e) => e.target.value)
        .throttle(500)
        .switchMap(query => getSomeAjaxObservable(query))
    };
  }

  render() {
    return (
      <div>
        <input onKeyPress={this.keypresses.next}/>
        <ul>{this.data.autoCompleteData.map(x => (<li>{x}</li>))</ul>
      </div>
    );
  }
}

I’m not sure if I’d recommend people use this pattern, but they certainly could and it’s pretty ergonomic which is nice. I prefer mixing Redux and RxJS, myself, as I think it’s better architecturally.

3reactions
mweststratecommented, May 3, 2016

@blesh indeed, standards are the best 😃. But as pointed out by @jimfb having observables / thennables in itself is not enough to solve the problem of over- or undersubscribing, nor does it allow for automatically subscribing to observables used by render.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Excel HACK: Change HORIZONTAL Data to VERTICAL (NO ...
How can you reconfigure a horizontal row to a vertical column? You can use a simple Excel hack to get this done.
Read more >
Horizontal Vs. Vertical Scaling: How Do They Compare?
With that being said, let's look at a simple breakdown of the advantages and disadvantages of vertical and horizontal scaling.
Read more >
Create a Data Visualizer diagram - Microsoft Support
Create a polished flowchart from an associated Excel workbook. Add, edit, or delete rows in Excel, and then refresh the diagram from Visio....
Read more >
Horizontal Scrolling in Web Design: How to Do It Well
Learn the advantages and drawbacks of horizontal scrolling in web design, and how to get started adding this functionality to your pages.
Read more >
Data Visualization – How to Pick the Right Chart Type? - eazyBI
The column chart is probably the most used chart type. This chart is best used to compare different values when specific values are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found