You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a React app that uses an authentication manager component a bit like the one in expo router demo app (AuthManager) to handle user authentication and route validation. The issue I'm facing is that the route component (here "AdminProjectPlanning") is being rendered before the AuthManager component validates the route, causing unnecessary re-renders.
Here's a simplified version of my AuthManager component :
constAuthManager: React.FC=()=>{const[routeValidated,setRouteValidated]=React.useState(false);constrootSegment=useSegments();constrouter=useRouter();constinstanceIsAuth=useAppStore((state)=>state.instanceState.isAuth);constinstanceUser=useAppStore((state)=>state.instanceState.user);// useEffect to handle route validation when isAuth is set and the route changesReact.useEffect(()=>{if(null!==instanceIsAuth){setRouteValidated(false);// Redirect unauthenticated users to the sign-in pageif(!instanceIsAuth&&'(sign-in)'!==rootSegment[0]){router.replace('/sign-in');}elseif(instanceIsAuth&&null!==instanceUser){// Redirect authenticated users to the correct role pageif(rootSegment[1]!==instanceUser.role){router.replace(`(app)/${instanceUser.role}`);}}setRouteValidated(true);}},[instanceIsAuth,instanceUser,rootSegment]);returnnull===instanceIsAuth ? (<ThemedActivityIndicator/>) : !routeValidated ? null : (<Slot/>);};
problem is that the AdminProjectPlanning component renders before the AuthManager component's useEffect runs to validate the route, resulting in unecessary API calls triggered and code rendered.
With the routeValidated state, I expect the route component not to render until the route validation is complete. However, the issue persists. How can I ensure that the route component only renders after the AuthManager validates the route?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm working on a React app that uses an authentication manager component a bit like the one in expo router demo app (AuthManager) to handle user authentication and route validation. The issue I'm facing is that the route component (here "AdminProjectPlanning") is being rendered before the AuthManager component validates the route, causing unnecessary re-renders.
Here's a simplified version of my AuthManager component :
problem is that the AdminProjectPlanning component renders before the AuthManager component's useEffect runs to validate the route, resulting in unecessary API calls triggered and code rendered.
With the routeValidated state, I expect the route component not to render until the route validation is complete. However, the issue persists. How can I ensure that the route component only renders after the AuthManager validates the route?
Beta Was this translation helpful? Give feedback.
All reactions