利用withRouter 中提供的replace方法来解决问题:将当前的location替换为其他的location
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';
class Login extends PureComponent {
static propTypes = {
...
history: PropTypes.object
}
constructor(props) {
super(props);
...
}
componentDidMount(){
this.props.history.replace('/login', '/')
}
render() {
return (
<div className='login-wrap'>
...
</div>
)
}
}
export default withRouter(Login)