How to access the Route path from its element?

Viewed 38

I'm using nested routes with react-router-dom v6. I would like to get hold of the path defined in a Route component within its element. Is there any other way than passing it as a prop to the element component itself?

Basically, how do I get / within Parent component even when I enter /subroute directly to the URL?

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Parent />}>
          <Route path="subroute" element={<Child />} />
        </Route>
      </Routes>
    </Router>
  );
}

Please see a codesandbox below.

Edit sad-haibt-wk9708

0 Answers
Related