加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – React’key’的有效符号是什么

发布时间:2020-12-15 05:07:30 所属栏目:百科 来源:网络整理
导读:反应关键支柱的有效符号是什么; div key="can i use spaces for example?"/div 在我的情况下,我想使用URL作为关键 const links = [ { label: 'foo label',href: 'https://example.com',},{ label: 'bar label',}]links.map( link = (a key={link.href} href=
反应关键支柱的有效符号是什么;
<div key="can i use spaces for example?"></div>

在我的情况下,我想使用URL作为关键

const links = [
    {
        label: 'foo label',href: 'https://example.com',},{
        label: 'bar label',}
]

links.map(
    link => (<a key={link.href} href={link.href}>{link.label}</a>)
);

这有效吗?我以为我可以使用一些哈希函数来首先传递href,但如果任何字符保证在键值中有效,这是一个毫无意义的步骤.

我问的原因是我在文档中找不到任何使用非字母数字字符作为键的示例,并且还明确说明,如果您没有用作键的ID您正在渲染的对象可以对其中某些部分进行哈希处理以创建密钥.虽然这可能是因为你不应该使用非常长的密钥,并且应该首先散列内容以截断它的大小,但似乎整个文档隐含地说只应使用字母数字字符作为密钥.

React密钥的要求最好在 reconciliation的文档中描述

In practice,finding a key is not really hard. Most of the time,the
element you are going to display already has a unique id. When that’s
not the case,you can add a new ID property to your model or hash some
parts of the content to generate a key. Remember that the key only has
to be unique among its siblings,not globally unique.

因此,关键应该是独特的(在其兄弟姐妹中)并且稳定.

因此,你的榜样非常合适.空间也应该没问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读