Highlight Possible Necessity of keys for Conditional Rendering
還沒有人認領這個 Issue。
- 主要語言
- JavaScript
- 星號
- 11.8k
- 分支
- 7.9k
- 平均合併
- 1 天 11 小時
- 30 天內合併 PR
- 11
描述
Motivation
Keys may be necessary outside the context of any "list" or array, especially when using conditional rendering. This isn't emphasized enough in the docs, and examples (therein and elsewhere) tend to lead to a false association between keys as having directly to do with map(), <li> elements, or JavaScript arrays. As I understand it, at its root, keys have to do with children, and type correspondence based on position during reconciliation.
Related issues such as https://github.com/reactjs/reactjs.org/issues/79 don't seem to directly address this.
Personal Experience
Most recommendations I find online seem to boil down to "use keys when using map()"(or <li> or arrays). It's rare to find a discussion related to conditional rendering and most people tend to be unaware of it.
I've been in a project wherein conditional rendering was heavily used, which led to a hard to find bug due to the absence of keys.
Code snippet in render()
return (
<div>
{ this.state.showCounter2 ?
(
<React.Fragment>
<CounterWithLifeCycleLog name={'myCounter1'}
counter={0} />
<CounterWithLifeCycleLog name={'myCounter2'}
counter={this.state.counter2} />
<CounterWithLifeCycleLog name={'myCounter3'}
counter={0} />
</React.Fragment>
) : (
<React.Fragment>
<CounterWithLifeCycleLog name={'myCounter1'}
counter={0} />
<CounterWithLifeCycleLog name={'myCounter3'}
counter={0} />
</React.Fragment>
)
The above code has a bug. When this.state.showCounter2 moves from true to false myCounter3 will be unmounted instead of myCounter2. Then the new myCounter3 will reuse the backing instance of myCounter2, maintaining its internal state.
It is true that this could be avoided by using the following instead:
&& <CounterWithLifeCycleLog name={'myCounter2'}
counter={this.state.counter2} />
The former example, however, is valid conditional rendering as well.
Proposed solution
An explicit mention of this confusion, an example involving conditional rendering (not using <li>, map(), or arrays), and perhaps a link to this blog post and perhaps the How to force remounting on React components related stackoverflow question.
Also, currently, all warnings related to keys seem to either have to do with arrays or iterators, perhaps a light warning when using conditional rendering could be helpful (though I can see how this will add way too much clutter), or some way to incorporate it into existing warnings.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先找到 React 文件中說明 keys、lists 和 reconciliation 的章節。使用提供的 conditional-rendering 範例來說明它與 map()、清單項目和陣列之間的區別。當文件清楚說明 keys 在 conditional rendering 中何時重要,並包含一個專用範例時,這項工作就完成了;任何警告變更都視為可選範圍。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- javascript, react
- 領域
- documentation
- Issue 類型
- 文件
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100