简体   繁体   English

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

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

I am getting error: 我收到错误消息:

Warning: Each child in an array or iterator should have a unique "key" prop. 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 Can someone please help me where i have to put key 有人可以帮我把钥匙放到哪里吗

Code for nav component: 导航组件的代码:

var React = require('React');
var $ = require('jquery');

var Menubar = React.createClass({
    getInitialState: function() {
        return {
            items: []
        }
    },
    componentWillMount: function() {
        var _this = this;
        this.serverRequest =
        $.post("/nav", {}, function(result) {
           _this.setState({
                items: result.data
            });
        })
   },
    render: function() {
        var item = this.state.items.map(function(data) {
            var ico = "fa fa-" + data.ico;
            return (
               <div id={data.sname} className="nav_item">
                    <div className="nav_icon">
                          <a href={data.url}>
                               <i className = {ico}>
                               </i>
                          </a>
                    </div>
               </div>   
        );
    });
    return (
        <div>{item}</div>
    )
  }
})
module.exports = Menubar;

In react whenever you use a map function that returns JSX, each returned elements should have a key attribute in order for react to render optimally. 在每当使用返回JSX的map函数时,在react中,每个返回的元素都应具有key属性,以使react最佳地呈现。 You can read more here: https://facebook.github.io/react/docs/multiple-components.html#dynamic-children 您可以在此处阅读更多信息: https : //facebook.github.io/react/docs/multiple-components.html#dynamic-children

You need to stick a key attribute in this line, usually I use the map index, but if each element has it's own unique id, you can use that instead 您需要在该行中保留一个key属性,通常我使用map索引,但是如果每个元素都有其自己的唯一ID,则可以改用该属性

<div id={data.sname} className="nav_item" key={index}>

Here is the corrected code. 这是更正的代码。 (note that the comment needs to be removed as you can't use comments like that in actual JSX) (请注意,由于您不能在实际的JSX中使用注释,因此需要删除注释)

  var item = this.state.items.map(function(data, index) { var ico = "fa fa-" + data.ico; return ( // ** this element needs a key property ** <div id={data.sname} className="nav_item" key={index}> <div className="nav_icon"> <a href={data.url}> <i className = {ico}> </i> </a> </div> </div> ); 

Hope this helps. 希望这可以帮助。

You should be aware that React uses a virtual DOM to see if there are any changes to be flushed to the browser. 您应该知道,React使用虚拟DOM来查看是否有任何要刷新到浏览器的更改。 To do this it needs to know exactly which element it is so that it can compare it next time the DOM changes as a result of state/prop change. 为此,它需要确切地知道它是哪个元素,以便下次由于状态/属性更改而导致DOM更改时可以对其进行比较。 This way react assigns id to each DOM element and compares the DOM for changes. 这样,react将为每个DOM元素分配ID,并比较DOM的更改。

The reason you are shown this warning is that, when there is a set of DOM elements dynamically generated(in your case via a loop), you are required to tell react in someway so that it can recognize it back when it re-renders your DOM. 出现此警告的原因是,当动态生成一组DOM元素时(在您的情况下是通过循环),您需要以某种方式告诉react,以便在重新渲染您的DOM时可以将其识别回来DOM。

So the key attribute on the root element is required. 因此, root元素上的key属性是必需的。 Its a default practice to use index of the array for this, but i suggest you not to use this( read here ). 这是使用数组index的默认做法,但是我建议您不要使用this( 在这里阅读 )。 I suggest you use data.sname in your case as i find it to be unique as you are assigning it to id . 我建议您使用data.sname ,因为当您将其分配给id ,我发现它是唯一的。

Your change would be something similar to this 您的更改将与此类似

// ..other pieces of code
return (
           <div id={data.sname} key={data.sname} className="nav_item">
// ..other pieces of code

You should specify a unique key for your element. 您应该为元素指定一个唯一键。 React is checking the key to identify and compare your element to determine if it needs to be re-rendered when a state change has been triggered in your tree. React正在检查用于识别和比较您的元素的键,以确定在树中触发状态更改时是否需要重新渲染它。

Using the index of an item as key is not a best practice, because if the list is modified then your node would not longer match the right element in the list and could produce unexpected results. 将项目的索引用作键不是最佳做法,因为如果修改了列表,则您的节点将不再与列表中的正确元素匹配,并且可能会产生意外结果。

If you are iterating over objects a good practice would be to use a property value as a key eg mapping users and use user.id as your unique key. 如果要遍历对象,则好的做法是将属性值用作键,例如,映射用户并使用user.id作为唯一键。

You should add a key to each child as well as each element inside children. 您应该为每个子项以及子项中的每个元素添加一个key

This way React can handle the minimal DOM change. 这样,React可以处理最小的DOM更改。

render: function() {
    var item = this.state.items.map(function(data, index) {
        var ico = "fa fa-" + data.ico;
        return (
           <div key={index} id={data.sname} className="nav_item">
                <div className="nav_icon">
                      <a href={data.url}>
                           <i className = {ico}>
                           </i>
                      </a>
                </div>
           </div>   
    );
});
return (
    <div>{item}</div>
)
}

暂无
暂无

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

相关问题 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 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. Not sure why 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 检查“ MovieResults”的渲染方法 - Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of `MovieResults` index.js:2178警告:数组或迭代器中的每个子代都应具有唯一的“键”属性。 -reactjs - index.js:2178 Warning: Each child in an array or iterator should have a unique “key” prop. - reactjs 警告:数组或迭代器中的每个子代均应具有唯一的“键”道具。 钥匙已经存在 - Warning: Each child in an array or iterator should have a unique “key” prop. Keys are already present 不知道为什么我会收到“警告:数组或迭代器中的每个子项都应该有一个唯一的“键”道具。” - Not sure why I am getting "Warning: Each child in an array or iterator should have a unique "key" prop." 警告:数组或迭代器中的每个子元素都应该有一个唯一的“key”属性。 反应两个按钮功能冲突 - Warning: Each child in an array or iterator should have a unique "key" prop. React two button function conflict 数组或迭代器中的每个子代都应具有唯一的“键”道具。 反应JS错误 - Each child in an array or iterator should have a unique “key” prop. React JS Error Reactjs-数组或迭代器中的每个子代都应具有唯一的“键”道具。 如何动态地做到这一点? - Reactjs - Each child in an array or iterator should have a unique “key” prop. How to do that dynamically? 警告:数组或迭代器中的每个孩子都应该有一个唯一的“键”道具。 检查 `ListView` 的渲染方法 - Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM