【发布时间】:2020-11-12 14:07:27
【问题描述】:
我正在使用react-router-dom 来保护整个应用程序。所有路由都受到 ProtectedRoute 组件的保护(参见下面的代码),如果用户未登录,该组件将重定向到外部 url,即单点登录 (SSO) 页面。
问题:
当用户转到“/home”时,他们会在被重定向到“external-login-page.com/”(登录页面)之前对受保护的路由有一个简短的了解(“闪光”)。如何避免闪烁,让用户只看到登录页面?
export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({
isAuthenticated,
...rest
}) => {
if (!isAuthenticated) { // redirect if not logged in
return (
<Route
component={() => {
window.location.href = 'http://external-login-page.com/';
return null;
}}
/>
);
} else {
return <Route {...rest} />;
}
};
【问题讨论】:
-
你的意思是代码以某种方式执行
else语句一小会儿? -
@thk_ 你的问题解决了吗?
标签: reactjs react-router react-router-dom