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

React router returning the same element

I am using react-router to display some pages

function App() {
  return (
    <Router>
     <Routes>
     <Route exact path="/" element={<HomePage />} />
     <Route  path ="about" component={<AboutPage />}  />
     </Routes>
    </Router>
  );
}

But the second path is either display a blanck page in abouve piece of code or display the same homepage if i use like below

<Routes>
     <Route exact path="/" element={<HomePage />} >
     <Route path ="about" component={<AboutPage />}  />
     </Route>
     </Routes>

</Router>

Which is the correct method to use to get the different pages?

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

>Solution :

The second path still needs to be rendered on the element prop. In react-router-dom@6 the Route components no longer have component, and render and children function props, they were replaced by the single element prop taking a ReactElement.

function App() {
  return (
    <Router>
     <Routes>
       <Route path="/" element={<HomePage />} />
       <Route path="about" element={<AboutPage />} />
     </Routes>
    </Router>
  );
}
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