简体   繁体   English

跟踪数组或迭代器中的每个子代均应具有唯一的“键”道具

[英]tracking down Each child in an array or iterator should have a unique “key” prop

I often get this error while doing stuff in react, and it's hell to try and track down where the problem is. 在做反应时,我经常会遇到此错误,而尝试找出问题所在是很困难的。 Does anyone have tips for digging through your UI to find these cases? 有没有人有挖掘您的UI来找到这些案例的提示?

Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `Constructor`. See https://..... /react-warning-keys for more information.

I've started just adding key={Math.random} to divs - is there any reason NOT to do this? 我刚刚开始将div key={Math.random}添加到div中-有什么理由不这样做吗? React just needs any unique key? React只需要任何唯一的密钥?

If this is the case, I'm not sure why the framework doesn't just do that internally; 如果是这种情况,我不确定为什么框架不只是在内部进行; they're already generating pseudonames for each of the DIVs it seems... 他们似乎已经为每个DIV生成了假名...

The error messages aren't very helpful in indicating where the problem is: 错误消息在指示问题出在哪里不是很有帮助:

warning @ react-dev.js:18780validateExplicitKey @ react-dev.js:9316validateChildKeys @ react-dev.js:9378createElement @ react-dev.js:9483render @ Editor.tsx:245_renderValidatedComponentWithoutOwnerOrContext @ react-dev.js:5943_renderValidatedComponent @ react-dev.js:5963ReactCompositeComponent__renderValidatedComponent @ react-dev.js:12346_updateRenderedComponent @ react-dev.js:5916_performComponentUpdate @ react-dev.js:5900updateComponent @ react-dev.js:5829ReactCompositeComponent_updateComponent @ react-dev.js:12346performUpdateIfNecessary @ react-dev.js:5777performUpdateIfNecessary @ react-dev.js:13036runBatchedUpdates @ react-dev.js:13734perform @ react-dev.js:15623perform @ react-dev.js:15623perform @ react-dev.js:13691flushBatchedUpdates @ react-dev.js:13752ReactUpdates_flushBatchedUpdates @ react-dev.js:12346closeAll @ react-dev.js:15689perform @ react-dev.js:15636batchedUpdates @ react-dev.js:8455enqueueUpdate @ react-dev.js:13781enqueueUpdate @ react-dev.js:13371enqueueSetState @ react-dev.js:13537ReactComponent.setState @ react-dev.js:5204(anonymous function) @ Editor.tsx:100v @ zepto.min.js:2S.onreadystatechange @ zepto.min.js:2
OptionPicker.tsx:44template.initValue spot_intro

The reason for that is called reconcilliation and it's explained in react's Multiple Components doc page : 其原因称为协调 ,在react的Multiple Components文档页面中进行了解释

When React reconciles the keyed children, it will ensure that any child with key will be reordered (instead of clobbered) or destroyed (instead of reused). 当React调和带键的子代时,它将确保所有带键的子代都将重新排序(而不是破坏)或销毁(而不是重用)。

If you use Math.random , you'll get a different id every time so react won't be able to do its trick. 如果您使用Math.random ,则每次都会获得一个不同的id,因此react将无法做到这一点。 This very example appeared in react's github issues and as Paul O'Shannessy pointed: 这个例子出现在react的github问题中,正如Paul O'Shannessy指出的那样:

  • key is not really about performance, it's more about identity (which in turn leads to better performance). 关键不在于性能,而在于身份(反过来又可以带来更好的性能)。 randomly assigned and changing values are not identity 随机分配且变化的值不是身份
  • We can't realistically provide keys without knowing how your data is modeled. 在不知道数据建模方式的情况下,我们无法实际提供密钥。 I would suggest maybe using some sort of hashing function if you don't have ids 我建议如果您没有ID,也许使用某种哈希函数
  • We already have internal keys when we use arrays, but they are the index in the array. 使用数组时,我们已经具有内部键,但是它们是数组中的索引。 When you insert a new element, those keys are wrong. 当您插入新元素时,这些键是错误的。

I'd suggest you read the aforementioned thread. 我建议您阅读上述主题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组或迭代器中的每个孩子都应该有一个唯一的“key” prop - Each child in an array or iterator should have a unique "key" prop 数组或迭代器中的每个子代在reactjs中都应具有唯一的“键”道具 - Each child in an array or iterator should have a unique “key” prop in reactjs 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具 - Warning :Each child in an array or iterator should have a unique “key” prop React - 数组或迭代器中的每个子节点都应该有一个唯一的“key”prop - React - Each child in an array or iterator should have a unique “key” prop 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具 - Warning: Each Child in an Array or Iterator Should Have a Unique “Key” Prop 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 li中已添加的关键道具 - Warning: Each child in an array or iterator should have a unique “key” prop.; key prop already added in li 数组或迭代器中的每个子代都应具有唯一的键道具-但我确实定义了一个键道具 - each child in an array or iterator should have a unique key prop - but i did define a key prop 持久警告:“即使已经设置了密钥,数组或迭代器中的每个子节点都应该具有唯一的'密钥'prop” - Persistent Warning: “Each child in an array or iterator should have a unique 'key' prop” even if key is already set 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查`单位`的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `Units` 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“搜索”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of 'search'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM