Hello i'm new to react development and recently react router has been updated to v 6 and that caused a lot of problem when i try to create an admin route. for example in v5 i used .
<AdminRoute exact path={${path}/makeAdmin
}>
but it does not work in v6. please help me.
Asked
Active
Viewed 639 times
2

Drew Reese
- 165,259
- 14
- 153
- 181

Md Yousuf Hossen
- 23
- 5
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jan 12 '22 at 04:45
1 Answers
1
The Route
component API and general routing changed significantly from version 5 to 6. Gone are custom route components, replaced by wrapper components that handle the business logic and return either the children
prop or an Outlet
(for nested Route
components), or a redirect (in the form of a Navigate
with replace
prop).
Example v5 custom Route
:
const AdminRoute = props => {
const isAdmin = /* admin business logic */;
return isAdmin ? <Route {...props} /> : <Redirect to="/" />;
};
...
<AdminRoute exact path={`${path}/makeAdmin`}>
<ProtectedAdminComponent />
</AdminRoute>
Example conversion to v6 using wrapper component:
import { Navigate, Outlet } from 'react-router-dom';
const AdminWrapper =() => {
const isAdmin = /* admin business logic */;
return isAdmin ? <Outlet /> : <Navigate to="/" replace />;
};
...
<Route element={<AdminWrapper />}>
<Route path={`${path}/makeAdmin`} element={<ProtectedAdminComponent />} />
</Route>

Drew Reese
- 165,259
- 14
- 153
- 181