什么是“钥匙"?连接反应路由器的每条路线变化都有哪些变化? [英] What is the "key" which changes on every route change with connected-react-router?
问题描述
当导航到路线的动作被触发时,动作会触发一个新状态,其中 router.location.pathname
根据浏览器的 history
发生变化.>
另一个属性也发生了变化:router.location.key
,变成一个新的随机字符串.
即使路径名本身没有改变(点击页面本身的链接),键
仍然更新.
key
属性的用途是什么?在哪些情况下,我希望我自己的状态有一个随机生成的 key
,它会在非常动作调度时更新?为什么它不是一个简单递增的数字?
connected-react-router
简单地存储来自 react-router
的位置对象,后者反过来创建使用 history 包的位置对象.在 history
的自述文件中,对 key
属性进行了描述:
位置还可能具有以下属性:
location.key - 表示此位置的唯一字符串(支持createBrowserHistory 和 createMemoryHistory)
它在内部使用(例如在 https://github.com/ReactTraining/history/blob/master/modules/createBrowserHistory.js 以在当前历史堆栈中查找位置)并且应该被视为 react-router
的实现细节.我怀疑随机密钥而不是递增序列号只是实现唯一 ID 的最简单方法(您不必存储当前序列号).
When an action for navigating to a route is triggered, an action triggers a new state where the router.location.pathname
changes according to the browser's history
.
Another property changes as well: router.location.key
, to a new random string.
Even when the pathname itself doesn't change (clicking on a link to a page from the page itself), the key
still updates.
What's the purpose of the key
property? In which situations would I want my own state to have a randomly generated key
which updates on very action dispatch? Why is it not a number which simply increments?
connected-react-router
simply stores the location object from react-router
which in turn creates the location object using the history package. In the readme of history
the key
property is described:
Locations may also have the following properties:
location.key - A unique string representing this location (supported in createBrowserHistory and createMemoryHistory)
It is used internally (e.g. in https://github.com/ReactTraining/history/blob/master/modules/createBrowserHistory.js to find locations in the current history stack) and should be treated as an implementation detail of react-router
. I suspect a random key instead of a incrementing sequence number was simply the easiest way to implement unique ids (you don't have to store the current sequence number).
这篇关于什么是“钥匙"?连接反应路由器的每条路线变化都有哪些变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!