Reactjs 未发生未登录用户的预期重定向到/登录页面
我正试图用React Router V4实现一个相当简单的场景,但未登录用户预期的重定向到a/login页面的情况并未发生 主要应用程序组件是:Reactjs 未发生未登录用户的预期重定向到/登录页面,reactjs,react-router-v4,Reactjs,React Router V4,我正试图用React Router V4实现一个相当简单的场景,但未登录用户预期的重定向到a/login页面的情况并未发生 主要应用程序组件是: export class App extends Component { render() { return ( <div className="app"> <Helmet titleTemplate="%s" </Helmet>
export class App extends Component {
render() {
return (
<div className="app">
<Helmet
titleTemplate="%s"
</Helmet>
<Navigation />
<Switch location={this.props.location} >
<Route path="/login" component={Login} />
<PrivateRoute exact path="/" component={HomePage} />
</Switch>
</div>
);
}
}
App.propTypes = {
location: PropTypes.object,
};
const mapStateToProps = createStructuredSelector({
location: makeSelectLocation(),
});
export default connect(mapStateToProps)(App);
PrivateRoute的实现是
class PrivateRoute extends Component {
render() {
const {
component: Component,
...props
} = this.props;
console.log(props.location)
return (
<Route
{...props}
render={props =>
false
? <Component {...props} />
: <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
}
/>
);
}
}
PrivateRoute.propTypes = {
component: PropTypes.func,
};
export default PrivateRoute;
加载localhost即/,我看到浏览器中的url更改为/login,但在Redux中只有@INIT和@router/LOCATION\u更改为路径名“/”
我知道被阻止的更新问题,所以我在代码库中删除了PureComponent的所有用法,以确保它不会在层次结构中被阻止
PrivateRoute正在通过connect使用redux获取登录状态。我已经删除了它,只需输入一个固定的值false来重定向到/login
PrivateRoute中的console.log是:
定义管线时使用“精确”
<Route exact path="/login" component={Login} />
降级为alpha-6。重定向现在可以工作了
- "react-router-redux": "5.0.0-alpha.8",
+ "react-router-redux": "5.0.0-alpha.6",
谢谢你的建议,但那没什么区别。PrivateRoute中的重定向似乎不起作用。