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 doesnt work when I try to render the components

I try to render the components with React Router, but it doesnt seem to work. I can’t find what I do wrong after the changes on React router.
I don’t get any errors. The components are just not rendering on the page.
I tried add BrowserRouter too, nothing changes.

My code

import './default.scss'
import { Routes ,Route  } from 'react-router-dom';
import MainLayout from './layouts/MainLayout';

import Homepage from './pages/Homepage';
import Registration from './pages/Homepage/Registration';

function App() {
  return (
    
      <Routes>
         <Route exact path="/" render={() => (
           <MainLayout>
             <Homepage/>
           </MainLayout>
         )}
          ></Route>
         <Route path="/registration" render={() => (
           <MainLayout>
             <Registration/>
           </MainLayout>
         )}
          ></Route>
         </Routes>
         
  );
}

export default App;

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 :

Guess your using react-route-dom v6, you can check out the guide upgrading from v5

Change route following

<Routes>
  <Route
    path="/"
    element={
      <MainLayout>
        <Homepage/>
      </MainLayout>
    }
  />
  <Route
    path="registration"
    element={
      <MainLayout>
        <Registration/>
      </MainLayout>
    }
  />
</Routes>

or using useRoutes

let element = useRoutes([
  // These are the same as the props you provide to <Route>
  {
    path: "/",
    element:
      <MainLayout>
        <Homepage/>
      </MainLayout> },
  {
    path: "registration",
    element:
      <MainLayout>
        <Registration/>
      </MainLayout>
  },
  // Not found routes work as you'd expect
  { path: "*", element: <NotFound /> },
]);
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