I've been building Links in my React app (using react-router-dom 4.3.1) using code like the following:
const { match } = this.props;
...
pages.map(page =>
<Link to={match.url+'/'+page.id()+'/info'}>
Go to info page for {page.title()}
</Link>)
as this seems to be the recommended practice (e.g. see ${match.url}/components https://reacttraining.com/react-router/web/guides/quick-start/example-nested-routing).
The problem I'm having:
If I'm at the following path:
/app/home
the links generated by the above are as expected:
- /app/home/1/info
- /app/home/2/info
- /app/home/3/info
- etc.
But if I load this (subtlely different) path (notice the trailing /):
/app/home/
then the generated links are wrong (notice the double / after home):
- /app/home//1/info
- /app/home//2/info
- /app/home//3/info
- etc.
In other words, the problem is that sometimes there's a trailing / and sometimes there is not.
When I'm building a link, do I need to manually check for a trailing / every time and strip it if it's there? Or is there some better best practice that I'm perhaps missing?
to={match.url+page.id()+'/info'?'/'in your link, it is better to use/app/homefor thematch.url. If you want to use/app/home/then remove the'/'from the Link/and sometimes there isn't. Presumably my app should work in both situations? Or I should just ignore one of them?