improve the phrasing for using "no keys" on https://reactjs.org/docs/lists-and-keys.html#keys
還沒有人認領這個 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.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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