1. 关于this.props.location与this.props.history.location该用哪个?
- 简单粗暴的结论
简单说就是,
😯 两者的区别就是this.props.location更可靠
👌 so, 最好直接用this.props.location,特别是在react 生命周期钩子函数中(如果在生命周期函数中用this.props.history.location可能得不到你期望的值)
- react-router官网中的描述
react-router官网中在描述history时说: history is mutable
在描述location时又有这样一句话:a location object is never mutated so yu can use it in the lifecycle hooks…
👆 就是官网推荐你直接通过this.props.location来获取location
如果不想知道为什么,this is the end ~ 如果有兴趣了解,就继续往下看吧
2. 用实例来理解history is mutable
现在我有如下配置,
【path: /myurl 】— 对应 — 【 React组件: myUrl 】
<Route path='/myurl' component={
MyUrl} />;
该路由对应的组件是这样的:
class MyUrl extends React