I have the following route configuration using react-router v3:
<Route component={App}>
<Route path="login" component={Login} />
<Route path="logout" component={Logout} />
<Route path="/" component={Admin}>
<IndexRoute component={Dashboard} />
<Route path="profile" component={Profile} />
</Route>
</Route>
I tried the following using v4:
<Router>
<div id="app">
<Match pattern="/login" component={Login} />
<Match pattern="/logout" component={Logout} />
<Match pattern="/" component={Admin} />
</div>
</Router>
and inside Admin
component:
<div id="admin">
<Match pattern="/" component={Dashboard} />
<Match pattern="/profile" component={Profile} />
</div>
The problem:
- When I visit
/login
,Admin
component is also matched. - I tried changing to
<Match pattern="/" exactly component={Admin} />
. It doesn't render the sub matches in theAdmin
component. So when I visit/profile
, it has no match.
Nesting is still a bit wonky with v4, but it is still in alpha so that shouldn't be too unexpected.
As you have defined your code, there isn't a good way to do what you want. One possible solution would be to have a
withAdmin
higher order component that will render your admin HTML for wrapped components.Which you would use like:
It isn't ideal, but it should work.
There is also the possibility that the final version will include a
<Match___>
component which accepts an array of patterns and only renders the component associated with the first matched pattern. That would be a real solution to your problem, but it doesn't exist yet.