Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Routes in react-router-dom@6 and take the path by useLocation() hook

function App() {
  const location = useLocation();
  const [activePlanet, setActivePlanet] = useState('/');

  return (
    <Routes location={location}>
      <Route
        path="/"
        element={
          <DefaultLayout pathName={location.pathname} setActivePlanet={setActivePlanet} activePlanet={activePlanet} />
        }>
        <Route index element={<Home activePlanet={activePlanet} />} />
        {routes.map((route, idx) => (
          <Route key={`route-${idx}`} path={route.path} element={route.element} />
        ))}
      </Route>
    </Routes>
  );
}
 
PREVIOUS NEXT
Tagged: #Routes #path #hook
ADD COMMENT
Topic
Name
3+7 =