Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
Nested routing react-router-dom v6 not working

I am new to React and I am following this project. But I am stuck on converting the nested routed in version6 of react-router-dom.

In V5 it is as follows:

App.js

  return (
      <div>
        <Header />
        <Switch>
          <Route exact path='/' component={HomePage} />
          <Route path='/shop' component={ShopPage} />
          <Route exact path='/checkout' component={CheckoutPage} />
          <Route
            exact
            path='/signin'
            render={() =>
              this.props.currentUser ? (
                <Redirect to='/' />
              ) : (
                <SignInAndSignUpPage />
              )
            }
          />
        </Switch>
      </div>
    );

ShopPage

      const ShopPage = ({ match }) => (
  <div className='shop-page'>
    <Route exact path={`${match.path}`} component={CollectionsOverview} />
    <Route path={`${match.path}/:collectionId`} component={CollectionPage} />
  </div>
);

I want to convert it in V6 and I had tried as follows:

My App.js

return (
  <div >
    <Header />
    <Routes>
      <Route path='/' element={<HomePage />} />
      <Route path='/shop' element={<ShopPage />} />
        
      <Route path='/checkout' element={<CheckoutPage />} />
      <Route path='/signin' element={<>
        {this.props.currentUser ?
          <Navigate to="/" />
          :
          <SignInAndSignUpPage />

        }
      </>
      }

      />

      <Route path='/signin' element={this.props.user ? <Navigate to="/" /> : <SignInAndSignUpPage />} />
     

    </Routes>
  </div>
);

My Shopage

    const ShopPage = () => {
    let { pathname } = useLocation();

    console.log(pathname);
   
    return (
        <div className='shop-page'>
            <Routes>
                <Route path={`${pathname}`} element={<CollectionsOverview />} />
                <Route path={`${pathname}/:collectionId`} element={<CollectionItems />} />
            </Routes>
        </div>
    )
};

In pathname console log I am getting /shop but the items are not showing. How can acheive nested routing in v6?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It looks like CollectionsOverview is what is rendered exactly on "/shop". Nested routes operate a little different in v6 than they did in v5. You don't need to get the current path/url to build nested routes/links, you can just use relative routes.

Example:

const ShopPage = () => {
  return (
    <div className='shop-page'>
      <Routes>
        <Route path="/" element={<CollectionsOverview />} />
        <Route path=":collectionId" element={<CollectionItems />} />
      </Routes>
    </div>
  );
};

Optionally, you can can convert ShopPage into a layout route.

Example:

import { Outlet } from 'react-router-dom';

const ShopPage = () => (
  <div className='shop-page'>
    <Outlet />
  </div>
);

App

return (
  <div >
    <Header />
    <Routes>
      <Route path='/' element={<HomePage />} />
      <Route path='/shop' element={<ShopPage />}>
        <Route index element={<CollectionsOverview />} />
        <Route path=":collectionId" element={<CollectionItems />} />
      </Route>
      <Route path='/checkout' element={<CheckoutPage />} />
      <Route
        path='/signin'
        element={this.props.currentUser
          ? <Navigate to="/" />
          : <SignInAndSignUpPage />
        }
      />
      <Route
        path='/signin'
        element={this.props.user
          ? <Navigate to="/" />
          : <SignInAndSignUpPage />
        }
      />
    </Routes>
  </div>
);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!