什么是“钥匙"?连接反应路由器的每条路线变化都有哪些变化?

编程入门 行业动态 更新时间:2024-10-25 16:27:19
本文介绍了什么是“钥匙"?连接反应路由器的每条路线变化都有哪些变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当导航到路线的动作被触发时,动作会触发一个新状态,其中 router.location.pathname 根据浏览器的 history 发生变化.>

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

即使路径名本身没有改变(点击页面本身的链接),键仍然更新.

key 属性的用途是什么?在哪些情况下,我希望我自己的状态有一个随机生成的 key,它会在非常动作调度时更新?为什么它不是一个简单递增的数字?

解决方案

connected-react-router 简单地存储来自 react-router 的位置对象,后者反过来创建使用 history 包的位置对象.在 history 的自述文件中,对 key 属性进行了描述:

位置还可能具有以下属性:

location.key - 表示此位置的唯一字符串(支持createBrowserHistory 和 createMemoryHistory)

它在内部使用(例如在 github/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 github/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).

更多推荐

什么是“钥匙"?连接反应路由器的每条路线变化都有哪些变化?

本文发布于:2023-11-13 06:36:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每条   路由器   钥匙   都有哪些   路线

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!