Skip to content

[V6] [Feature] useResolvedPath should return a function to resolve the path #8172

@fxOne

Description

@fxOne

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 isActive detection in the NavLink component, which doesm't work because I can't call useResolvedPath in a loop with several different to params
  • a hook that return the resolved path for current RouteContext as used in useResolvedPath

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions