score:1
Issue
So I've gathered that a user is on the "/login" path and authenticate and you redirect them to the home path "/"
. If I'm reading the code correctly it's at this point the RequireAuth
wrapper mounts and checks the current user. The initial currentUser
state is undefined, or in other words is a falsey value, and <Navigate to="/login"/>
is rendered and the user is redirected back to the login page. This unmounts the RequireAuth
but there's possibly the lingering enqueued state update.
Solution
Move the RequireAuth
outside App
. I don't think this is the direct cause of your issue, but it's simply an anti-pattern to declare React components within other React components. Each time App
rerenders you actually are declaring a new RequireAuth
component and any component state will be lost.
To resolve the routing issue, add an indeterminant "pending" state that is either authenticated nor unauthenticated, and don't commit to rendering the outlet or redirect until the wrapper component confirms the currentUser
auth status. Using undefined
works for this as it is neither a user object nor null with there is no user.
function RequireAuth() {
const currentUser = useAuth();
if (currentUser === undefined) {
return null; // or loading indicator, etc...
}
return currentUser ? <Outlet /> : <Navigate to="/login"/>;
}
Source: stackoverflow.com
Related Query
- react router v6 won't switch to Home Page after Login Page: Warning: Can't perform a React state update on an unmounted component
- Using React Router to Redirect to next page after successful login
- How to redirect to home in react router dom after login with redux?
- React router & Switch - page is not loading after refreshing
- Redirect to home page after React Azure AD login
- Redirecting to home page after login conflicts with removing protected routes for logged in users | React
- Accessing data passed from NavLink after redirecting from home page to a specific page using react router
- I cant use framer motion when using <navigate/> react router dom v6 to auth my login page
- How to hide navbar in login page in react router
- React Router v4: Cant load page on GitHub pages
- React Router - Stay at the same page after refresh
- Redirect to login page in react router 4
- Navigate to page after authentication using React Router v4
- React Router v4 - Redirect to home on page reload inside application
- Redirect to Home after Login on React Redux Application
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- How to hide a sidebar from a login page in react router
- React Router doesn't load the new page component after redirecting via navigate
- React Router - Open Link on new Tab and Redirect to Home Page
- React Router scroll page to the top after transition
- React Router Still Renders Previous Page after Routing to New Component
- React Router v4 login check after hitting Refresh on browser
- How do i make a react app redirect to the login page after registration?
- How can I add multiple path for home page with react router dom 6?
- React Router works only after refreshing the page
- react redirect to another page after login
- React - Routing & Redirect after login to attempted page
- react-router-Dom: Blank page after redirect multiple user on login in React
- React Router - Preserve current route after page refresh
- React Router issue. Whenever login button is clicked. I am directed to a blank page with the homepage link appended to the login page link is made
More Query from same tag
- How to describe TS interface for history.goBack in React?
- Build react app multiple times with different environment variables
- The same component render two times
- How to pass updated state to a child component on rendering of the page?
- Conditional rendering an input inside a map
- React CSSTransition with Route renders next page first then works with rendering next page again
- Can we make changes on screen/browser without using hooks in ReactJS. (In case of functional component)?
- How to make a phone call in reactjs
- Get Specific Dates from Array of dates in React Datetime
- onChange event won't fire in .map function React
- how to stop re-render in reactjs?
- Webscraping from React web application after componentDidMount
- Is React's setState asynchronous or something?
- React with Typescript - is not a function
- Alternative to Reflux for client and server (individual sessions)
- Webpack and babel-loader not resolving `ts` and `tsx` modules
- how to use onChange on array of objects to add data in ReactJS
- Please help me create an image slider in react.js?
- Using React to render flash messages from Express
- Using PropTypes with Redux Components
- comparing state and prop value - error: cannot read property 'state' of undefined
- Material-UI v5 override MuiSwitch input
- React-Bootstrap-Table-Next Sort Alphanumeric Strings on Dynamic Table
- how can i upgrade it to react-router-dom v6
- react datepicker custom css styling
- tabnavigator and stacknavigator problem-you should only render one navigator
- Reactjs ES6 Access function outside of class
- push history in useEffect causes redirect in other routes
- how to set useState data on an object
- Converting blob from database to an image javascript