首页 > 解决方案 > 从子节点获取父路由路径(react-router-V6)

问题描述


V6相关问题!

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

如果我点击导航所有作品并且我在(/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>
}

编辑:

我找到了一种解决方案,但在行动之前获得完整路径的反向方法......

  const resolver = useResolvedPath(to);
  //@ts-ignore
  const match = useMatch(resolver.pathname);

使用这个钩子...

标签: react-routerreact-router-dom

解决方案


推荐阅读