admin管理员组文章数量:1316980
I'm using react and I came across a problem, albeit an aesthetic problem and not a functional one.
I am generating react-route
s from an API of names. The route works fine, but as the names have spaces, they appear in the url as: example/lookup/David%20Attenborough
Example: <Link to='{/lookup/' + props.data.name}>{props.data.name}</Link>
Is there a clever way I can remove the spaces: example/lookup/DavidAttenborough
or even with +
or -
to replace spaces without losing the structural integrity of react-router
.
I'm using react and I came across a problem, albeit an aesthetic problem and not a functional one.
I am generating react-route
s from an API of names. The route works fine, but as the names have spaces, they appear in the url as: example./lookup/David%20Attenborough
Example: <Link to='{/lookup/' + props.data.name}>{props.data.name}</Link>
Is there a clever way I can remove the spaces: example./lookup/DavidAttenborough
or even with +
or -
to replace spaces without losing the structural integrity of react-router
.
- 4 you could do something like this: props.data.name.split(' ').join(''); Or use regex – floor Commented Apr 6, 2018 at 14:54
- 1 You could use .split(' ').join('-') on props.data.name, but I'm not sure if your router will break – Ron Nabuurs Commented Apr 6, 2018 at 14:55
- 1 @floor you beat me too it – Ron Nabuurs Commented Apr 6, 2018 at 14:55
- Possible duplicate of When to encode space to plus (+) or %20? – Ruan Mendes Commented Apr 6, 2018 at 15:06
- 1 It's called a slug, you can do this with slugify or others. What do you mean by losing the structural integrity of react-router? – Gabriel Bleu Commented Apr 6, 2018 at 15:46
3 Answers
Reset to default 2Actually, +
is not valid as an encoding for spaces in the path, only in the query string. See When to encode space to plus (+) or %20? and https://github./ReactTraining/react-router/issues/2407
You cannot do what you are asking
You can use regex for Putting (- or +) in the url example
let name = props.data.name;
name = name.replace(/\s+/g, '-');
const url = `/lookup/${name}
<Link to={url}>{props.data.name}</Link>
you can either add + or -
This works for me:
<Link to={`/lookup/${props.data.name.split(` `).join(`-`).toLowerCase()}`}>{props.data.name}</Link>
本文标签: javascriptRemove 392039 from URLReactRouterStack Overflow
版权声明:本文标题:javascript - Remove '%20' from URL - React-Router - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742010724a2412834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论