gpt4 book ai didi

react-router-v4 - 什么是 "key",它在每次使用 connected-react-router 更改路由时都会发生变化?

转载 作者:行者123 更新时间:2023-12-01 01:43:53 26 4
gpt4 key购买 nike

当导航到路线的 Action 被触发时, Action 会触发一个新状态,其中 router.location.pathname根据浏览器的history更改.

另一个属性也发生了变化:router.location.key , 到一个新的随机字符串。

The diff between two states, showing how key changes alongside a pathname.

即使路径名本身没有改变(点击页面本身的链接),key还在更新。

The diff between two states, showing how only the key changes.
key的目的是什么?属性(property)?在什么情况下我希望我自己的状态有一个随机生成的 key非常 Action 调度的哪些更新?为什么它不是一个简单递增的数字?

最佳答案

connected-react-router简单地存储来自 react-router 的位置对象这反过来使用 history 创建位置对象包裹。在 history 的自述文件中key属性描述:

Locations may also have the following properties:

location.key - A unique string representing this location (supported in createBrowserHistory and createMemoryHistory)



它在内部使用(例如在 https://github.com/ReactTraining/history/blob/master/modules/createBrowserHistory.js 中查找当前历史堆栈中的位置)并且应该被视为 react-router 的实现细节。 .我怀疑随 secret 钥而不是递增序列号只是实现唯一 ID 的最简单方法(您不必存储当前序列号)。

关于react-router-v4 - 什么是 "key",它在每次使用 connected-react-router 更改路由时都会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53237729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com