高阶组件中的withRouter
, 作用是将一个组件包裹进Route
里面,所以withRouter
的作用就是, 如果我们某个东西不是一个Router(路由组件)
, 但是我们要依靠它去跳转一个页面, 比如点击退出登录, 返回登录页面, 这时候就可以使用withRouter
来做.
import { withRouter } from 'react-router-dom';
function HeadTop({ history,location,match }) {
const loginOut = () => {
history.replace("/login")
}
});
}
return (
<div>
<button onClick={loginOut}>
退出登录
</button>
</div >
)
}
export default withRouter(HeadTop)