Say I have an array that contains objects with route props like this and permission.
const myRoutes = [
{ route: '/',
element: <Home/>
permission: 'admin' },
{ route: '/',
element: <About/>
permission: 'agent' },
]
In this case, both Home and About component are Private route and it goes like
<Routes>
<Route path="/" element={<PermissionCheck />}>
{myRoutes.map((route)=> ({
<Route path={route.path} element={route.element} permission={permission}/>
})}
</Route>
</Routes>
To make a permission check abstracted, I would like to check the permission with given permission
prop to each route, <Home />
and <About />
in this case.
I have come up with one way to solve this by creating a hook that accepts permission and call it in each route element but is there any better way to do the same?