reactjs / reactjs/react.dev

improve the phrasing for using "no keys" on https://reactjs.org/docs/lists-and-keys.html#keys

未关闭
#3,732 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
11.8k
派生
7.9k
平均合并
1 天 11 小时
30 天内合并 PR
11

描述

https://reactjs.org/docs/lists-and-keys.html#keys currently has a section that causes confusion about React's behaviour when omitting key attributes, currently reading:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using indexes as keys."

The emphasized sentence has led to folks thinking that React knows what a specific map()'s iteration index is, and that it'll use that, rather than that the index talked about is the child index of the element in the vdom. To remove that confusion and make it explicit that React is using an internal index, I would recommend changing the phrasing here to:

"We don’t recommend using indexes for keys if the order of items may change. This can negatively impact performance and may cause issues with component state. Check out Robin Pokorny’s article for an in-depth explanation on the negative impacts of using an index as a key. If you choose not to assign an explicit key to list items then React will default to using element index in the virtual DOM tree as keys."

It might be even more high value to also add one or more sentences to explain that this means that the behaviour for consecutive maps that don't use keys vs. incorrectly use iteration indices as keys will lead to different behaviour, which folks right now mistakenly assume would behave the same.

E.g.

render() {
  const { text, additional } = this.state;
  const content = text.map(t => <p>{t}</p>);
  const addenda = additional.map(t => <p className="add">{t}</p>);
  return <section>{content}{addenda}</section>;
}

will work, but:

render() {
  const { text, additional } = this.state;
  const content = text.map((t,i) => <p>{t}</p>);
  const addenda = additional.map((t,i) => <p className="add" key={i}>{t}</p>);
  return <section>{content}{addenda}</section>;
}

will not, and if you are familiar enough with React already to not need the docs, you know why, but if you're not and you read the docs, it is entirely understandable if you come away convinced that this should work after reading the current text.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

https://reactjs.org/docs/lists-and-keys.html#keys 的 Keys 部分开始,并结合所提供的示例检查关于省略 Keys 的措辞。明确 React 使用元素在 virtual DOM tree 中的索引,并决定是否也应添加连续 map 的示例;文档应准确区分这两种行为。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, react
领域
documentation
Issue 类型
文档
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。