简体   繁体   English

带键的ReactJS动态子级

[英]ReactJS dynamic child with keys

Since the latest version of ReactJS we have a warning that asks to add keys for dynamic children. 从最新版本的ReactJS开始,我们有一个警告,要求添加动态子代的键。 I had some troubles to found which component was generating this warning... I first assumed that only the child that were generated dynamically in a "for loop" would throw this warning (since it's "generated dynamically"). 我很难找到哪个组件正在生成此警告...我首先假设只有在“ for循环”中动态生成的子级才会抛出此警告(因为它是“动态生成”的)。 Finally, I found that every components of our application throw this error because we have children everywhere (even if we don't have a "for loop") :( 最后,我发现应用程序的每个组件都会引发此错误,因为我们到处都有孩子(即使我们没有“ for循环”):

We are using CoffeeScript and I'm asking myself if we are using ReactJS in the correct way: 我们正在使用CoffeeScript,我问自己是否在以正确的方式使用ReactJS:

DOM = React.DOM
myComponent = React.createClass
  render: ->
    DOM.div {className: "app", ref: "app"},
      DOM.div {className: "child1"}, "This is a test" # throw warning
      DOM.div {className: "child2", key: "child2"}, "Hello" # don't throw warning

Here child1 is throwing a warning unless we add a key attribute. 除非我们添加key属性,否则child1会在此处发出警告。 Is this behavior normal ? 这是正常现象吗? Are we doing something wrong ? 我们做错什么了吗? What I mean is that we need to add keys to more than 500 components/children which is a hard and boring work... 我的意思是,我们需要为500个以上的组件/子代添加密钥,这是一项艰巨而无聊的工作...

Also, the warning says that the function "undefined" is throwing it. 此外,警告还指出函数“ undefined”正在抛出该错误。 So I need to dig around 10min to 1h everytime to check where to correct things... :( 所以我需要每次挖掘大约10分钟到1小时,以检查在哪里纠正问题... :(

Best regards, Kursion 最好的问候,Kursion

There are 2 things going on here, so let's separate them… 这里发生了两件事,所以让我们将它们分开…

Here child1 is throwing a warning unless we add a key attribute. 除非我们添加key属性,否则child1会在此处发出警告。 Is this behavior normal ? 这是正常现象吗? Are we doing something wrong ? 我们做错什么了吗? What I mean is that we need to add keys to more than 500 components/children which is a hard and boring work... 我的意思是,我们需要为500个以上的组件/子代添加密钥,这是一项艰巨而无聊的工作...

  1. As @FakeRainBrigand mentioned, this sample code shouldn't be warning about keys. 正如@FakeRainBrigand所提到的,此示例代码不应警告有关键。 and in fact it doesn't - http://jsfiddle.net/zpao/5KSah/ . 实际上不是-http://jsfiddle.net/zpao/5KSah/ The way we made this warning work is by detecting if an array was passed as an argument. 我们进行此警告的方式是通过检测是否将数组作为参数传递。
  2. If you have 500 children, then I have a lot of trouble believing you don't have an array in there. 如果您有500个孩子,那么我很难相信您没有孩子。

Also, the warning says that the function "undefined" is throwing it 另外,警告说函数“ undefined”正在抛出它

This is because React uses a property called displayName (specified on the object passed to createClass ) to build that warning. 这是因为React使用了一个名为displayName的属性(在传递给createClass的对象上指定)来构建该警告。 When using JSX, we can autogenerate that property for you based on assignment. 使用JSX时,我们可以根据分配为您自动生成该属性。 It's not always perfect but it usually works well enough. 它并不总是完美的,但通常效果很好。 When using CoffeeScript, you may want to specify this property yourself. 使用CoffeeScript时,您可能需要自己指定此属性。

/** @jsx React.DOM */
var MyComponent = React.createClass({ ... });
// becomes
var MyComponent = React.createClass({displayName: 'MyComponent', ... });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM