I am trying to create a fully protected React application. Only Login
page should be available without login. However it has a completely different layout than rest of the application.
I am using simply <Outlet />
for rendering the app layout. However on route change, the Menu
and Header
are also rerendering. Is there a solution how to prevent the rerender of these components?
App
const App = () => {
return (
<Routes>
<Route path="/" index={true} element={<LoginRoute />} />
<Route element={<ProtectedRoutes />}>
<Route path="dashboard" index={true} element={<Dashboard />} />
<Route path="article" element={<ArticleRoute />} />
<Route path="article/add" element={<ArticleAddRoute />} />
<Route path="article/:id" element={<ArticleEditRoute />} />
</Route>
<Route path="*" element={<ErrorPage />} />
</Routes>
);
};
ProtectedRoutes
const ProtectedRoutes = () => {
const { userData } = useAuth();
const location = useLocation();
return userData ? (
<Container>
<NavContainer>
<Menu />
</NavContainer>
<Main>
<Header />
<div>
<Outlet />
</div>
</Main>
</Container>
) : (
<Navigate to="/" state={{ from: location, status: 'unauthorized' }} replace />
);
};
Should I separate the LoginRoute
component completely out of the Routes
and showing it conditionally if userData
exists or is there any other solution?
2
Answers
I guess that they are rerendering because you use
useLocation
If you are concerned about the
Menu
andHeader
being possibly rerendered along with theProtectedRoutes
when the routes change, then I’d suggest separating them out into another layout component so their rerendering is decoupled fromProtectedRoutes
component rerenders.Example: