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>