React-router: v4: Should paths merge in child routes?

Created on 1 Feb 2017  路  3Comments  路  Source: ReactTraining/react-router

I just wanted to make sure this is by design and intentional.

Let's say I want to have a path /admin/products

This is how my intuition thought it should work.

// Main component
<Switch>
    <Route path='/admin' component={Admin} />
    <Route path='/foo' component={Foo} />
</Switch>
// Admin component
<Switch>
    <Route path='/products' component={AdminProducts} />
</Switch>

Instead this is what seems to work

// Main component
<Switch>
    <Route path='/admin' component={Admin} />
    <Route path='/foo' component={Foo} />
</Switch>
// Admin component
<Switch>
    <Route path='/admin/products' component={AdminProducts} />
</Switch>

I kind of thought that putting routes inside other routes would result path to be a combined result of parent and child paths.

Is there a reason it's not like that? Am I the only one with this intuition?

Most helpful comment

@lestard See #4459 and #4539

All 3 comments

Currently there is no relative path support, so a <Route>'s path should be absolute to the root. You can either hardcode it like in your example or concatentate your path with match.path.

const Admin = ({ match }) => (
  <Switch>
    <Route path={`${match.path}/products`} component={AdminProducts} />
  </Switch>
)

Is there any plan of supporting relative paths?
I had the same expectation for relative paths in child components.
The current implementation leads to a tighter coupling between parent and child components because if you change the path of the parent component you have to touch the child components too, potentially multiple levels below in the routing hierarchy.

@lestard See #4459 and #4539

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maier-stefan picture maier-stefan  路  3Comments

stnwk picture stnwk  路  3Comments

ryansobol picture ryansobol  路  3Comments

wzup picture wzup  路  3Comments

imWildCat picture imWildCat  路  3Comments