This is my code to load routes (react router dom 5) based on user object in context.
function App() {
return (
<UserContextProvider>
<BrowserRouter>
<Navigation/>
</BrowserRouter>
</UserContextProvider>
);
}
Navigation code. Here I conditionally load routes.
function Navigation() {
const { contextState } = useUserContext();
const routes = contextState.user ? routesAuthenticated : routerUnauthenticated;
return (
<Switch>
{routes.map((route, index) => {
return (
<Route
key={index}
path={route.path}
exact={route.exact}
render={(props: RouteComponentProps) => (
<route.component name={route.name} {...props} {...route.props} />
)}
/>
);
})}
</Switch>
);
}
You see that based on context user object I load different routes. Routes are simple config files:
export const routerUnauthenticated: IRoute[] = [
{
path: '/register',
name: 'Register page',
component: RegisterPage,
exact: false,
},
{
path: '/login',
name: 'Login page',
component: LoginPage,
exact: false,
}...
Problem is I am on route http://localhost:3000/login
and after succesfull login I got blank page with route http://localhost:3000/login
.
Before login I have 3 routes login/register/home.
After login I have 5 routes dashboard/profile...
What I am trying to do is after successfull login get to /dashboard
route but because of my idea with dynamic routes loading I can't figure how to navigate.
Login is simple fake function in my context:
try {
setContextState({ type: ContextActions.LOGIN_IN_PROGRESS });
setTimeout(() => {console.log({ userId: 1, username: '[email protected]' });
setContextState({ type: ContextActions.LOGIN_SUCCESS, payload: { user: { userId: 1, username: '[email protected]' } } });
}, 3000);
} catch(error) {
setContextState({ type: ContextActions.LOGIN_ERROR });
}
Why not just load all the routes? You can use a custom Route component to see if the contextState.user exists Kind of like a protected route which redirects to /login or /register if a non authenticated user goes to that route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With