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.

Rx pushing data through React-Router

See original GitHub issue

I have a react App with the data flow being driven by RxJS -> here

Short of it is I subscribe to my data store which I then pump through via the props:

Model.subject.subscribe((appState) => {
  React.render(
    <App {...appState}/>,
    document.getElementById('app')
  );
});

Now is there a way I can merge the above subscription into the Router.run…?

Router.run(routes, function (Handler) {
  React.render(
    <Handler />,
    document.getElementById('app')
  );
});

I’ve tried the bellow the router still works but the appState isn’t been passed through!!

 Model.subject.subscribe((appState) => {
  Router.run(routes, function (Handler) {
    React.render(
      <Handler {...appState}/>,
      document.getElementById('app')
    );
  });
});

Any pointers would be superb thanks 😃

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
BerkeleyTruecommented, May 26, 2015

What you want is a for an observer to be notified when both Router.run and Model.subject publish new values.

This calls for… combineLatest!

First, make Router.run into an observable

const RouterObs = Rx.Observable.create(observer => {
    Router.run(routes, location, (Handler, state) => {
      observer.onNext({ Handler, state });
    });
  });
};

Now sprinkle in a dash of combineLatest!

const subscription = Rx.Observable.combineLatest(
  Model.subject,
  RouterObs,
  function((appState, { Handler, State }) => ({
    appState,
    Handler,
    State
  }))
  .subscribe(({ appState, Handler, State }) => {
    React.render(
      <Handler {...appState}/>,
      document.getElementById('app')
    );
  });
});

Bam! Rx-ified Router Soup!

Hope that answers your question.

P.S. Adds some spice by introducing flatMap and Rx-ifying render ala Thundercats.js (disclaimer: I am the author of Thundercats)

const ObservableRender = new Rx.AnonymousObservable(function (observer) {
    let instance = null;
    instance = React.render(Comp, DOMContainer, (err) => {
      if (err) { return observer.onError(err); }
      if (instance) { observer.onNext(instance); }
    });
    observer.onNext(instance);
  });
});

const subscription = Rx.Observable.combineLatest(
  Model.subject,
  RouterObs,
  function((appState, { Handler, State }) => ({
    appState,
    Handler,
    State
  }))
  .flatMap(({ appState, Handler, State }) => {
    return ObservableRender(
      <Handler {...appState}/>,
      document.getElementById('app')
    );
  })
  .subscribe(reactRoot => {
    console.log('booya! React rendered!');
  });
});
0reactions
Cmdvcommented, May 27, 2015

so solution sat up in <RouteHandler {...this.props} /> so you can pass your state into the handler then you need to call it’s props to pass to the rest of the app there.

var App = React.createClass({
  render: function () {
    return (
      <div>
        <header>
          <ul>
            <li><Link to="app">Home</Link></li>
            <li><Link to="about">About</Link></li>
            <li><Link to="login">Login</Link></li>
          </ul>
        </header>
        <RouteHandler {...this.props} />
      </div>
    );
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Rx pushing data through React-Router · Issue #1230 - GitHub
Short of it is I subscribe to my data store which I then pump through via the props: Model.subject.subscribe((appState) => { React.render( <App ......
Read more >
React router, pass data when navigating programmatically?
Is there a way to send params (object) along when navigating? There are other options I can think of, but wonder if passing...
Read more >
How to pass additional data while redirecting to a route in React
In this article, we will see how we can pass extra data while redirecting to a different route. So let's get started. Using...
Read more >
RxJS with React Hooks for state management - LogRocket Blog
Reactive programming is an event-based paradigm that allows us to run asynchronous sequences of events as soon as data is pushed to a...
Read more >
Chapter 12. React on the server and integrating React Router
Today you'll find that many apps (like yours) use a robust client-side application to manage the UI and a remote (usually REST) API...
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