Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

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


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...