您应该编写一个authroute hoc,而不是编写用于身份验证的硬编码路由,
const AuthRoute = ({component: Component, ...rest}) => {
if(isAuth) {
return <Route {...rest} component={Component} />
}
return <Redirect to="/" />
}
并使用它
<React.Fragment>
{/* <Redirect exact from="/" to="/dashboard" /> */}
<AuthRoute path="/" exact component={props => <Dashboard {...props} />} />
<AuthRoute path="/dashboard" exact component={props => <Dashboard {...props} />} />
<AuthRoute path="/settings/" exact component={props => <Settings {...props} />} />
</React.Fragment>
您不想验证的任何路由都将被记为普通路由。