1.为啥使用它?
非路由级组件,例如:公共组件...
然后在这样的组件当中不能通过this.props拿取history、location、match等信息的
2.写法
(1)引入 (2)用withRouter() 包裹组件(高阶组件嘛)
3.withRouter是如何更新的
Important Note
withRouter does not subscribe to location changes like React Redux’s connect does for state changes. Instead, re-renders after location changes propagate out from the component. This means that withRouter does not re-render on route transitions unless its parent component re-renders. If you are using withRouter to prevent updates from being blocked by shouldComponentUpdate, it is important that withRouter wraps the component that implements shouldComponentUpdate.
结论是:
组件的re-render,父组件re-render也行
如何触发re-render呢?
1.组件状态的改变
2.手动shouldComponentUpdate
当使用withRouter时遇到更新时机不对时,也可以把这种情况列入考量范围