-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Closed
Description
We have an inspector with a submenu that displays different items depending on the selected item. The inspector uses react-router.
One requirement is that if the selected item changes and the current menu item is not available for the new item, a redirect should be applied.
My current solution is the following:
function pathMatch(items, pathname ) {
for (const {value} of items) {
const resolvedPath = resolvePath(value, pathname);
if (resolvedPath.pathname === pathname) {
return true;
}
}
return false;
}
const location = useLocation();
const navigate = useNavigate();
useEffect(() => {
if (!pathMatch(items, location.pathname)) {
navigate('..', {replace: true});
}
}, [items, location.pathname, navigate]);But sometimes there are more elements in the url which breaks my solution since location.pathname returns the current full URL. eg,: ´pagePath/inpectorPage/newItemDialog´
Currently I see 2 solutions for my problem:
- an implementation like the
isActivedetection in theNavLinkcomponent, which doesm't work because I can't calluseResolvedPathin a loop with several differenttoparams - a hook that return the resolved path for current
RouteContextas used inuseResolvedPath
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels