我有这个元素的商店:
loggedIn:false
我想做的是阻止用户尚未登录的任何页面。当他成功登录时,布尔值在其他组件上变为true。
为了阻止他访问应用程序的其他链接,我创建了一个名为Refresh Route的组件,该组件可以执行以下操作:
import React from "react";
import { connect } from "react-redux";
import { Route, Redirect } from "react-router-dom";
const RefreshRoute = ({ component: Component, ...rest }) => {
const canGo=this.props.loggedIn;
window.alert(canGo)
return (<Route
{...rest}
render={props =>
canGo ? (
<Component {...props} />
) : (
<Redirect
to={{
pathname: "/login"
}}
/>
)
}
/>)
}
const mapStateToProps = state => {
return {
loggedIn: state.atuth.loggedIn
}
}
export default connect(mapStateToProps)(RefreshRoute);
我的问题是道具为空,我不知道为什么。
这是我称为“刷新路线”的组件:
const Routes = () => (
<main>
<Switch>
<Route exact path='/' component={Main} />
<Route exact path='/customers' component={Customers} />
<Route exact path='/customers/:market' component={CustomersByMarket} />
<Route exact path='/customers/:market/:customer' component={Customer} />
<Route exact path='/customers/:market/:customer/:environment' component={MessageDashboard} />
<RefreshRoute exact path='/home' component={Main}/>
<Route exact path='/addPermission' component={AddPermissions}/>
<Route exact path='/excludePermission' component={RemovePermissions}/>
<Route exact path='/login' component={SignIn}/>
</Switch>
)
导出默认路由;
富国沪深
炎炎设计
相关分类