I have a React application with React Router set up to handle navigation. In my application, I want to implement dynamic routing based on the user's role, where different pages are shown for the same route depending on the user's role. Currently, I have a working solution using a roleRedirect object that maps roles to corresponding paths, but I suspect there might be a more efficient and elegant way to achieve this functionality.
I would appreciate any guidance on how to improve this routing logic.
Below is the code snippet, ofcourse I would be using redux state to get user roles, but for the sake of simplicity I wrote this:
const roleRedirect = {
admin: "/admin",
agent: "/user",
};
const App = () => {
let userRole = "admin";
return (
<ThemeProvider theme={theme}>
<Routes>
<Route path="/" element={<FullLayout />}>
<Route path="/" element={<Navigate to={roleRedirect[userRole]} replace />} />
<Route path="/login" element={<Login />} />
<Route path="/admin" element={<AdminDashboard />} />
<Route path="/user" element={<UserDashboard />} />
</Route>
</Routes>
</ThemeProvider>
);
};