Get parent route path from children (react-router-V6)

Viewed 1796

V6 related question !

I have nested routing and in child component I have tab component... I need to get path of parent before using navigate(...)

If I click on navigate all works and I`m switching between (/View/test1 and /View/test2 and /View/test3 )

But I need to get parent path before i click on any my tab... Because my tab component use relative path i need to highlight if it is active or not..

Is there a hook to get parent route path? I was searching for something like usegetParrentPath() or some function that can build that route...

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>
}

Edited:

I found one solution but reverse way to get fullpath before action...

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

Using this hook...

0 Answers
Related