从子级获取父级路由路径(react-router-V6)

dam*_*ami 9 react-router react-router-dom


V6相关问题!

我有嵌套路由,在子组件中我有选项卡组件...我需要在使用 navigator(...) 之前获取父级的路径

如果我单击导航所有作品,我将在(/View/test1 和 /View/test2 和 /View/test3 )之间切换

但我需要在单击任何选项卡之前获取父路径...因为我的选项卡组件使用相对路径,我需要突出显示它是否处于活动状态。

有没有获取父路由路径的钩子?我正在寻找诸如 usegetParrentPath() 之类的东西或一些可以构建该路线的函数...

function GetTabs() {
  return [
    {
      label: "Test1",
      path: `Test1`,
    },
    {
      label: "Test2",
      path: `Test2`,
    },
    {
      label: "Test3",
      path: `Test3`,
    },
  ] as RouterTabItemType[];
}

function App(){
<Routes>
  <Route path "/View/*" element={<View/>
<Routes>
}

function View(){
<Routes>
  <Route path "test1" element={<Test1/> // Full path will be /View/test1
  <Route path "test2" element={<Test2/> // Full path will be /View/test2
  <Route path "test3" element={<Test3/> // Full path will be /View/test3
<Routes>
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我找到了一种解决方案,但在采取行动之前以相反的方式获取完整路径......

  const resolver = useResolvedPath(to);
  //@ts-ignore
  const match = useMatch(resolver.pathname);
Run Code Online (Sandbox Code Playgroud)

使用这个钩子...