【发布时间】:2019-07-03 10:27:36
【问题描述】:
我正在尝试使用 react 构建一个网络应用程序,但我有点碰壁了。
问题是,我不希望任何已经登录的用户访问登录页面。
我尝试使用render方法和componentDidMount方法重定向它们,但是由于在componentDidMount之前调用了render方法,所以在重定向之前登录页面会闪烁。
这些是我的代码。
我正在使用 redux 和 firebase 来控制组件状态并进行身份验证。
class CenterPanel extends Component {
render() {
const { user }=this.props;
if(user!=null) { //this is how I tried to redirect
return ( //
<Redirect to='/somewhereNotHere' />
);
}
return ( //this flashes out before redirection
<div className={cx('center_panel')}>
<div className={cx('button')}>
login
</div>
</div>
);
}
componentDidMount() {
const { changeUserStatus }=this.props;
firebase.auth().onAuthStateChanged(
(user) => changeUserStatus(user)
);
}
}
不胜感激任何建议。 提前谢谢。
【问题讨论】:
-
尝试将 window.location 设置为 '/somewhereNotHere'
-
我试过 window.location 和 history。两者都重定向成功,但是重定向前登录页面仍然闪现。我希望已经登录的用户完全无法访问登录页面。
-
当用户不为空时只返回
null而不是<div ... />树。但是在返回 null 之前重定向... -
你从哪里得到
user道具值?
标签: reactjs