最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dynamic path NavLink React - Stack Overflow

programmeradmin2浏览0评论

I'm looking for a way to make the "to" property of NavLink react dynamic.

let url = 'localhost'=='localhost'? 'foo':'';
<Router>
 <NavLink to={url+'/'}>Home</NavLink>
</Router>

Work but with each new render react it adds a new url to the existing one. http://localhost/foo After render http://localhost/foo/foo ...

thanks

I'm looking for a way to make the "to" property of NavLink react dynamic.

let url = 'localhost'=='localhost'? 'foo':'';
<Router>
 <NavLink to={url+'/'}>Home</NavLink>
</Router>

Work but with each new render react it adds a new url to the existing one. http://localhost/foo After render http://localhost/foo/foo ...

thanks

Share Improve this question asked Sep 3, 2018 at 11:31 gyorgio88gyorgio88 5171 gold badge5 silver badges8 bronze badges 1
  • Not related to your question but if you dont plan to change the value of the url in your code you should use const and not let. – Cata John Commented Sep 3, 2018 at 11:50
Add a ment  | 

1 Answer 1

Reset to default 5

Try putting a slash in FRONT of the URL, too...

<NavLink to={'/'+url+'/'}>Home</NavLink>

Having no URL in front means "relative to the current path", while a URL in front means "relative to the domain name"

Or maybe a better way to acheive the same result in your case would be to put the slash in front of foo - eg:

let url = 'localhost'=='localhost'? '/foo':''; // <=== added slash 
<Router>
 <NavLink to={url+'/'}>Home</NavLink>
</Router>
发布评论

评论列表(0)

  1. 暂无评论