admin管理员组文章数量:1221351
What's the difference between props.location.pathname
and props.match.url
in react-router-dom
?
From their DOCS:
match.url
(string) The matched portion of the URL. Useful for building nested
<Link>
slocation
A location object to be used for matching children elements instead of the current history location (usually the current browser URL).
So far, I've ony seen them with exact same values.
Example:
If my route is matched in this url:
/search/searchValue?category=whatever
And I want to remove the query strings and go to:
/search/searchValue
Should I use one over the other or they both will work?
What's the difference between props.location.pathname
and props.match.url
in react-router-dom
?
From their DOCS: https://reacttraining.com/react-router/web/api/location
match.url
(string) The matched portion of the URL. Useful for building nested
<Link>
slocation
A location object to be used for matching children elements instead of the current history location (usually the current browser URL).
So far, I've ony seen them with exact same values.
Example:
If my route is matched in this url:
/search/searchValue?category=whatever
And I want to remove the query strings and go to:
/search/searchValue
Should I use one over the other or they both will work?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 12, 2019 at 16:50 cbdevelopercbdeveloper 31.4k44 gold badges197 silver badges393 bronze badges1 Answer
Reset to default 18location.pathname
represents the root-relative url.
match.url
represents the matched portion of the URL, so maybe a portion of location.pathname
.
Given these two components :
function Home({match, location}) {
return (
<div>
{match.url}
<br/>
{location.pathname}
</div>
);
}
function App() {
return (
<Router>
<Route path="/" component={Home}/>
</Router>
);
}
If you go to /something
, then
- match.url will be / (because the matched portion of the URL is
/
) - location.pathname will be /something (the relative-root URL)
Here is the example on stackblitz.
In your example, it depends whether your route is matching the exact path or not (https://reacttraining.com/react-router/web/api/Route/exact-bool).
If it's not the case (and you only want to retrieve /search/searchValue
) then you should use match.url
because location.pathname
could be more than /search/searchValue
-> /search/searchValue/something
.
本文标签: javascriptDifference between locationpathname and matchurl in reactrouterdomStack Overflow
版权声明:本文标题:javascript - Difference between location.pathname and match.url in react-router-dom? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739266609a2155627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论