理解react中经常使用的withRouter

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时遇到更新时机不对时,也可以把这种情况列入考量范围

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值