我有一个代码示例,我试图重定向尚未登录的用户。我正在使用外部身份验证,以便通过第三方身份验证系统将用户发送到登录。我知道react-routes有Redirect
选项,我知道它们只重定向到路径名。有没有办法让它重新定向发生在window.assign
,它将用户立即重定向到另一个页面?
先感谢您!
const ProtectedRoute = (auth, component: Component, ...rest) => {
return <Route
{...rest}
render={props => auth.isAuthenticated()
? <Component {...props} />
: <Redirect
to={{
pathname: 'http://example.com/',
state: { from: props.location },
}}
/>
}
/>;
};
您可以评估组件的componentDidMount
钩子中的日志记录道具,并使用window.location重定向到完全不同的URL。例如:
componentDidMount() {
if(auth.isAuthenticated()) {
return <Component {...props} />
}
window.location.assign('http://example.com/');
}