When using React Router 6 to create a "protected route", I see the protected page's content flash briefly before the redirect to the login page.
I would assume this must be a well-known problem. Is there a solution for this?
ProtectedRoute:
const ProtectedRoute = ({
redirectPath = '/login',
children
}) => {
const { user } = UserAuth();
if (!user) {
return <Navigate to={redirectPath} replace />;
}
return children
? children
: <Outlet />;
};
export default ProtectedRoute;
AppRouter:
const AppRouter = () => (
<Routes>
<Route path="/" element={<LoginPage />} />
<Route path="login" element={<LoginPage />}/>
<Route element={<ProtectedRoute />} >
<Route path="account" element={<AccountPage />} />
</Route>
</Routes>
);
export default AppRouter;