![](/img/trans.png)
[英]How to get current 'page' and pass as param in React Router v6
[英]How to get the last part of the current URL with react router v6?
如何使用反應路由器 v6 獲得當前 url 的最后一部分?
例如:
如果當前路徑是https://localhost:4200/example/example-details
,那么我想這樣檢查
const location = useLocation();
if (location.pathname === '/example-details')) {
// do some stuff here
}
..但這總是返回完整路徑。
它適用於location.pathname.includes('/example-details')
,但我很好奇是否有反應路由器的方法,就像 Angular 路由器一樣。
檢查
location.pathname.includes('/example-details')
const location = useLocation();
const [pathName, setPathName] = useState(null) ;
useEffect(() => {
if(location) {
let tmp = location.pathName.slice(location.pathName.lastIndexOf("/") , location.pathName.length) ;
setPathName(tmp) ;
}
}, [location])
const match = useMatch('/example/:lastPart')
const value = match?.props.lastPart;
if (!!value) {
// do some stuff here
}
使用 useMatch,您可以將模板應用於當前位置,並在返回的 object 中提取一些部分作為道具。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.