Migrating from react-router-dom v5 to v6, there is some problem to show page element when using v6 Route function
previously on v5 I used these code:
App.js
import routes from "../../routes/routes";
import { HashRouter, Route, Switch } from "react-router-dom";
.....
export default function App () {
return (
<HashRouter>
<Switch>
routes.map((route, idx) => {
return (
route.component && (
<Route
key={idx}
path={route.path}
exact={route.exact}
name={route.name}
render={(props) =>
(<route.component {...props} />)
}
/>
)
);
})}
</Switch>
</HashRouter>
);
}
routes.js
import React from 'react';
const Home = React.lazy(() => import('../pages/home/Home'));
const Home2 = React.lazy(() => import('../pages/home/Home2'));
const Routes = [
{
path: '/home',
exact: true,
name: 'Home',
component: Home
},
{
path: '/home2',
exact: true,
name: 'Home2',
component: Home2
}
]
export default Routes;
on v5 it can run correctly but it fails to run on v6
I already changed Switch
into Routes
and used element
to render, here is some change I made
<Routes>
{routes.map((route, idx) => {
return (
route.component && (
<Route
key={idx}
path={`/app` + route.path}
exact={route.exact}
name={route.name}
element={
<route.component {...props} />
}
/>
)
);
})}
</Routes>
I also tried to change element
with component
but it still doesn’t work, is there something I missed that might can show correct page?
New contributor
gatauaaja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.