[英]React Router and RxJS data flow
I have a react App with the data flow being driven by RxJS -> here 我有一个React App,数据流由RxJS驱动-> 这里
I subscribe to my data store with RxJS and pass it through the app as a prop: 我使用RxJS订阅了我的数据存储,并将其作为道具通过应用传递:
Model.subject.subscribe((appState) => {
React.render(
<App {...appState}/>,
document.getElementById('app')
);
});
bellow is how react-router normally renders it self: 下面是react-router通常如何自我呈现:
Router.run(routes, function (Handler) {
React.render(
<Handler />,
document.getElementById('app')
);
});
This is what I have so far which is rendering react-router fine but the data appState
doesn't go through <handler />
but it logs out fine prior to React.render
到目前为止,这就是我使
appState
-Router正常显示的内容,但是数据appState
并未通过<handler />
进行<handler />
但在React.render
之前可以React.render
Router.run(routes, function (Handler) {
Model.subject.subscribe((appState) => {
console.log(appState); // the state passes here
React.render(
<Handler {...appState}/>, // the state doesn't pass here
document.getElementById('app')
);
});
});
Now is there a way I can merge the RxJS data subscription and put it through React-Router? 现在有一种方法可以合并RxJS数据订阅并将其通过React-Router放置吗?
Here is the solution in full, pay attention to <Handler {...appState}/>
which will pass the data into the app but you then need to pass that data via props <RouteHandler {...this.props} />
这是完整的解决方案,请注意
<Handler {...appState}/>
,它将把数据传递到应用程序中,但是您随后需要通过props <RouteHandler {...this.props} />
传递数据<RouteHandler {...this.props} />
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>
);
}
});
var routes = (
<Route name="app" path="/" handler={App}>
<Route name="about" handler={About}/>
<Route name="login" handler={Login}/>
<DefaultRoute handler={Home}/>
</Route>
);
Router.run(routes, function (Handler) {
Model.subject.subscribe((appState) => {
React.render(
<Handler {...appState}/>,
document.getElementById('app')
);
});
});
Are you sure that your handlers are expecting props in the way that you are spreading them in the Handler
component? 您确定您的处理程序正在以在
Handler
组件中传播道具的方式期望道具吗?
Either way, the way you've implemented it will only render anything if there's first a route change and then a change to the observable. 无论哪种方式,您实现的方式都只会在首先更改路线然后更改可观察对象的情况下呈现任何内容。 If there's a route change but no subsequent observable change, nothing will be rendered.
如果发生路线更改,但随后没有可观察到的更改,则不会呈现任何内容。
You want to render both when a route change happens, and when a change happens in the observable. 您希望在发生路线更改时以及可观察对象中发生更改时都呈现。 Try something like this instead:
尝试这样的事情:
var CurrentHandler = null;
var currentAppState = {};
Router.run(routes, function (Handler) {
CurrentHandler = Handler;
render();
});
Model.subject.subscribe((appState) => {
currentAppState = appState;
render();
});
function render() {
// Cannot render before we have a handler
if (!CurrentHandler) return;
React.render(
<CurrentHandler {...currentAppState} />,
document.getElementById('app')
);
}
EDIT 编辑
What you're doing is that you have an App
component which is the parent route handler for your other handlers ( Home
, Login
and About
). 您正在做的是拥有一个
App
组件,该组件是其他处理程序( Home
, Login
和About
)的父路由处理程序。 So when the route changes to /login
, your App
component will be called, and it will get appState
spread out as props. 因此,当路由更改为
/login
,将调用您的App
组件,并将appState
作为appState
散布开。 App
then renders RouteHandler
which then renders the Login
component (since that's what the route matches). 然后,
App
渲染RouteHandler
,然后渲染Login
组件(因为这就是路由所匹配的)。 But Login
won't get appState
as props, because you're not passing them on from App
. 但是
Login
不会将appState
用作道具,因为您不是从App
传递它们。
If you change App
to render <RouteHandler {...this.props} />
instead of <RouteHandler />
you will pass on all of App
s props to RouteHandler
which will then pass them on to Login
. 如果将
App
更改为呈现<RouteHandler {...this.props} />
而不是<RouteHandler />
,则将所有App
道具传递给RouteHandler
,然后将其传递给Login
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.