admin管理员组文章数量:1425663
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 notlet
. – Cata John Commented Sep 3, 2018 at 11:50
1 Answer
Reset to default 5Try 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>
本文标签: javascriptdynamic path NavLink ReactStack Overflow
版权声明:本文标题:javascript - dynamic path NavLink React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745457191a2659157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论