I’m using react-router-dom v6 to control the route of my React Js app.
Here are the specifications:
I’m creating the
AuthenticationRouteandPrivateRoutecomponents.The
AuthenticationRoutecomponent is used to wrap pages that the user doesn’t need to authenticate for examplesSignIn,SignUp,ForgotPassword, andResetPasswordpages.The
PrivateRoutecomponent is used to wrap private pages (authentication is needed) for example Home page. Inside thePrivateRoute, there are some layouts. One of them is called theDashboardlayout to wrap theDrawer(Sidebar/Navigation) component and theHomepage.If the user has not logged in via the
SignInpage, the app would return theSignInpage. If the user has logged in, the app would return theHomepage.
Here are the current conditions:
Note: The check sign (✅) represents the conditions I want while the cross sign (❌) represents the error or unwanted conditions.
All of the specifications above are met. ✅
The first time user runs the app, the
SignInpage is returned because the user has not logged in. ✅If the user has not logged in and typed “/” route to the address bar (to access the
Homepage) via theSignInpage, the app will not redirect the user to theHomepage instead of returning theSignInpage. ✅If the user successfully logged in via the
SignInpage, the app would return theHomepage (with “/” route). ✅If the user has logged in and typed “/sign-in” route to the address bar (to access the
SignInpage) via theHomepage, the app return error: ❌
Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
at Navigate (https://5xxrw.csb.app/node_modules/react-router/index.js:247:12)
at AuthenticationRoute (https://5xxrw.csb.app/src/components/Routes/AuthenticationRoute.jsx:21:26)
at Routes (https://5xxrw.csb.app/node_modules/react-router/index.js:306:18)
at App
at Router (https://5xxrw.csb.app/node_modules/react-router/index.js:266:18)
at BrowserRouter (https://5xxrw.csb.app/node_modules/react-router-dom/index.js:284:18)
The app should navigate back the user to the Home page (“/” route) instead of returning the error.
Here is the code for the AuthenticationRoute:
function AuthenticationRoute(props) {
const { children } = props;
const userProfile = readUserProfileFromLocalStorage();
return userProfile ? <Navigate replace to="/sign-in" /> : children;
}
and here is the code for the PrivateRoute:
function PrivateRoute(props) {
const { children } = props;
const userProfile = readUserProfileFromLocalStorage();
return userProfile ? (
<Dashboard>{children}</Dashboard>
) : (
<Navigate replace to="/sign-in" />
);
}
Here is the playground: https://codesandbox.io/s/stackoverflow-auth-private-routes-5xxrw
I did a similar thing using react-router-dom v5 but didn’t return the error. Everything was fine.
So, what’s the solution for this case?
Advertisement
Answer
There is an issue on your AuthenticationRoute component. You are redirecting the user to /sign-in when userProfile is defined, which causes an infinite loop since its the same page. It should be navigating to /
function AuthenticationRoute(props) {
const { children } = props;
const userProfile = readUserProfileFromLocalStorage();
return userProfile ? <Navigate replace to="/" /> : children;
}