Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SyntaxError: Expected corresponding JSX closing tag for <Route> with react-router-dom v6

I want my react app to have urls that look like localhost:3000/projects/construction and localhost:3000/services/landscape.

The docs for react-router-dom say you can nest routes like so:

function App() {
  return (
    <Routes>
      <Route path="invoices" element={<Invoices />}>
        <Route path="sent" element={<SentInvoices />} />
      </Route>
    </Routes>
  );
}

Why then does my app fail with an Expected corresponding JSX closing tag for <Route> error when I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<BrowserRouter>
 <Routes>
  <Route index element={<Home />} />
  <Route path='/projects' element={<Projects />}>
   <Route path='/construction' element={<Construction />}>
  </Route>
  <Route path='/services' element={<Services />}>
   <Route path='/landscape' element={<Landscape />}>
  </Route>
  <Route path='/about' element={<About />} />
  <Route path='/contact' element={<Contact />} />
  <Route path='*' element={<Navigate to='/' replace />} />
 </Routes>
</BrowserRouter>

>Solution :

The Services and Landscape routes are missing the closing tag. They can self-close, or have the closing tag added.

<BrowserRouter>
  <Routes>
    <Route index element={<Home />} />
    <Route path="/projects" element={<Projects />}>
      <Route path="/construction" element={<Construction />}></Route>
      <Route path="/services" element={<Services />} />   // <-- close
      <Route path="/landscape" element={<Landscape />} /> // <-- close
    </Route>
    <Route path="/about" element={<About />} />
    <Route path="/contact" element={<Contact />} />
    <Route path="*" element={<Navigate to="/" replace />} />
  </Routes>
</BrowserRouter>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading