我正在处理一个个人项目,无法检查用户是否已登录,因此当他们尝试转到/ login时,它将把他们重定向到主页。目前,我的代码执行以下操作:
我无法弄清楚如何使用默认状态渲染组件,然后由于状态更改而突然重定向,从而解决该问题。谢谢
Routes.js
const Routes = (props) => {
props.checkIfSignedIn();
return(
<Router>
<Switch>
<Route exact path='/' component={App}/>
<AuthorizedRoute path="/signup" component={SignUp}/>
<AuthorizedRoute path="/signin" component={SignIn}/>
<Route component={InvalidPage}/>
</Switch>
</Router>
);
};
const mapDispatchToProps = (dispatch) => {
return{
checkIfSignedIn: () => dispatch(checkIfSignedIn())
};
};
export default connect(null, mapDispatchToProps)(Routes);
Run Code Online (Sandbox Code Playgroud)
AuthorizedRoute.js
class AuthorizedRoute extends React.Component{
constructor(props){
super(props);
this.state = {
isLoggedIn: false
};
};
render(){
const { component: Component, ...rest } = this.props;
return(
<Route {...rest} render={props => …Run Code Online (Sandbox Code Playgroud)