I have upgraded to react-router-dom v6 I have been using RouteComponentProps for mapping over routes as below but was wondering how to inplement this in v6
<Switch>
{routes.map((route, index) => {
return (
<Route
key={index}
exact={route.exact}
path={route.path}
render={(routeProps: RouteComponentProps<any>) => (
<route.component {...routeProps} />
)}
/>
);
})}
</Switch>
I know that Switch is now replaced with Routes, but tnot sure what replaces RouteComponentProps
>Solution :
Version 6 does not have Switch and has removed the render function too.
Instead of Switch, use Routes.
Intead of the render method, use element which is a React.ReactNode .
<Routes>
<Route path="/" element={<Dashboard />} />
<Route path="messages" element={<DashboardMessages />} />
<Route path="tasks" element={<DashboardTasks />} />
<Route path="about" element={<AboutPage />} />
</Routes>;
Only if <route.component /> is a React.ReacNode.
<Routes>
{routes.map((route, index) => {
return (
<Route key={index} path={route.path} element={<route.component />} />
);
})}
</Routes>;
Reference:
https://reactrouter.com/docs/en/v6/components/route