react useHistory() 返回上一页
react 使用useHistroy()可以返回上一页:
需要引入:
import { withRouter } from 'react-router-dom';
然后定义:
goBack = () => { this.props.history.goBack(); }
最后使用:
<button onClick={this.goBack}>返回上一页</button>
完整的组件示例:
import React from 'react';
import { withRouter } from 'react-router-dom';
class MyComponent extends React.Component {
goBack = () => {
this.props.history.goBack();
}
render() {
return (
<div>
<h1>MyComponent</h1>
<button onClick={this.goBack}>返回上一页</button>
</div>
);
}
}
export default withRouter(MyComponent);