I'm trying to get a layout system with react-router. If I understand the docs correctly using IndexRoute is equal to nesting the routes under each other.
But In the following code when I use IndexRoute the child routes aren't being nested. but if
When I nest them with the right tags <Route><sub-route/></Route>
it works as expected:
//THIS WORKS
export default function(history) {
return (
<Router history={history}>
<Route component={App}>
<Route path="/" component={Home}>
<IndexRedirect to="slides"/>
<Route path="slides" component={SlideShow}/>
<Route path="posts" component={Posts}/>
<Route path="questions" component={Questions} />
<Route path="questions/:id" component={Question} />
</Route>
</Route>
</Router>
)
}
//DOSEN'T GET NESTED
export default function(history) {
return (
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Home}/>
<Route path="slides" component={SlideShow}/>
<Route path="posts" component={Posts} />
<Route path="questions" component={Questions} />
<Route path="questions/:id" component={Question} />
</Route>
</Router>
)
}
To illustrate this, if I use IndexRoute and take out this.props.children
from the home component the children will still get rendered.
Here is the home component:
class Home extends Component{
render(){
//const {SlideShow, Posts} = this.props
return(
<div>
<h1>HOME PAGE!</h1>
<span><Link to="/slides">to slides</Link></span>
<span><Link to="/posts">to posts</Link></span>
<span><Link to="/questions">to question</Link></span>
<div>{this.props.children}</div>
</div>
)}
}
My second question is regarding the IndexRedirect. As you can see I'm using it to essentially redirect the user to the /slides route. But I would rather like not to use redirect and instead use slides under home without a path. Something like this:
<Router history={history}>
<Route component={App}>
<Route path="/" component={Home}>
<Route component={SlideShow}/>
<Route path="posts" component={Posts}/>
<Route path="questions" component={Questions} />
<Route path="questions/:id" component={Question} />
</Route>
</Route>
</Router>
Is that possible?
I'm baiscally trying to achieve something like the following: Using React-Router with a layout page or multiple components per page
Only I don't quite understand the example given there as it doesn't use IndexRoute and seem to follow a different pattern.