admin管理员组文章数量:1414628
I'm learning react, and I try to create some routes, I have this code on my entry point:
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory } from 'react-router';
import App from './app/Components/AppComponent';
import SupervisoryReport from './app/Components/SupervisoryReportComponent';
import Topmenu from './app/Components/TopmenuComponent';
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" ponent={App}>
<IndexRoute ponent={SupervisoryReport} />
<Route path="test">
</Route>
</Route>
</Router>,
document.getElementById('app')
);
This works fine. What I need now is when I navigate to /test
to still render the Supervisory report, and add some stuff. But apparently, it only renders what is inside the current route, and not upper routes.
Any idea how to do that?
EDIT
Here is more explain what I want to do:
Let's suppose I have a route /supervisory-report
which have 20 pages inside like: /supervisory-report/page-1
, /supervisory-report/page-2
, etc
every one of that pages must have a ponent.
then I have some other routes, like '/test'.
Is there a way for all pages inside Supervisory Report, to show the same ponent, instead of rendering again in each ponent for each route?
I'm learning react, and I try to create some routes, I have this code on my entry point:
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, Link, IndexLink, browserHistory } from 'react-router';
import App from './app/Components/AppComponent';
import SupervisoryReport from './app/Components/SupervisoryReportComponent';
import Topmenu from './app/Components/TopmenuComponent';
ReactDOM.render(
<Router history={browserHistory}>
<Route path="/" ponent={App}>
<IndexRoute ponent={SupervisoryReport} />
<Route path="test">
</Route>
</Route>
</Router>,
document.getElementById('app')
);
This works fine. What I need now is when I navigate to /test
to still render the Supervisory report, and add some stuff. But apparently, it only renders what is inside the current route, and not upper routes.
Any idea how to do that?
EDIT
Here is more explain what I want to do:
Let's suppose I have a route /supervisory-report
which have 20 pages inside like: /supervisory-report/page-1
, /supervisory-report/page-2
, etc
every one of that pages must have a ponent.
then I have some other routes, like '/test'.
Is there a way for all pages inside Supervisory Report, to show the same ponent, instead of rendering again in each ponent for each route?
Share Improve this question edited Mar 1, 2016 at 14:53 Pablo asked Mar 1, 2016 at 14:31 PabloPablo 10.7k18 gold badges59 silver badges80 bronze badges2 Answers
Reset to default 4If you want every route to contain SupervisoryReport
inside of App
, you don't need to have this logic in your routing. I think it would be better to modify your App ponent's render method:
return (
<div>
<SupervisoryReport>
{this.props.children} // if you want the ponent to be inside
</SupervisoryReport>
{this.props.children} // if you want the ponent to be after
</div>);
And then your routing can be simplified to:
<Route path="/" ponent={App}>
<Route path="test" ponent={TestComponent} />
</Route>
I think it only makes sense to include SupervisoryReport
in your route config if you plan on swapping it out when the route changes. If this is the case, you can try something like:
<Route path="/" ponent={App}>
<Route path="other" ponent={OtherComponent}/>
<Route path="*" ponent={SupervisoryReport}>
<Route path="test" ponent={TestComponent} />
</Route>
</Route>
Note that you must have the * path last, as react will not check for any routes once it finds a match.
Edit: To solve your problem, I would try something like:
<Route path="/" ponent={App}>
<Route path="test" ponent={TestComponent}/>
<Route path="supervisory-report" ponent={SupervisoryReport}>
<Route path="page-1" ponent={PageOneComponent}/>
<!-- etc -->
</Route>
</Route>
It seem from your code there is no ponent that is attached to the path "test". Your Route should be like this
<Route path="/" ponent={App}>
<IndexRoute ponent={SupervisoryReport} />
<Route path="test" ponent={TestComponent}/>
</Route>
本文标签: javascripthierarchy in react RouterStack Overflow
版权声明:本文标题:javascript - hierarchy in react Router - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745190647a2646893.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论