最近在看 react-router v4
的 api
,在 https://reacttraining.com/react-router/web/api/Link/to-object 中看到一段代码,
<Link to={{
pathname: '/courses',
search: '?sort=name',
hash: '#the-hash',
state: { fromDashboard: true }
}}/>
这里的state: { fromDashboard: true }
,我完全没看懂,
百度了很多,没有找到,
刚开始以为是点击这个 Link
时会改变 state
做了尝试,发现并不是,
我还看了 Link
的源码,也没有发现什么奥秘,
所以由于好奇心的驱使,我提出了这个问题,
想知道这个state到底是什么意思,或者是举例有错误?
求解答。
原生的
history.pushState()
方法可以传入一个 state,有些浏览器会把它保存起来,重启浏览器的时候恢复 state:https://developer.mozilla.org/en-US/docs/Web/API/History_API