我有以下路由配置:
return (<div>
<Router>
<div>
<Route path='/login' component={LoginPage}/>
<EnsureLoggedInContainer>
<Route path='/abc' component={abc} />
</EnsureLoggedInContainer>
</div>
</Router>
</div>
);
确保登录容器是:
import React from 'react';
import { connect } from "react-redux";
class EnsureLoggedInContainer extends React.Component
{
componentDidMount() {
if ( !this.props.isLoggedIn )
{
// this.props.history.push('/login');
this.context.router.push('/contact');
}
}
render() {
// console.log(this.props);
if ( this.props.isLoggedIn )
{
return this.props.children;
}
else
{
return null;
}
}
}
const mapStateToProps = (state,ownProps) => {
return{
isLoggedIn : state.isLoggedIn,
// currentURL : this.props
}
}
export default connect(mapStateToProps)(EnsureLoggedInContainer);
但是,历史推送:this.props.history.push('/login');
不起作用。这里历史不存在。
如果我使用这样的配置:
<Route component={EnsureLoggedInContainer}>
<Route path='/myjs' component={MyjsPage} />
</Route>
我遇到的问题是:
Warning: You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored
reactjs 中最好的身份验证方式是什么?