2017-09-27 39 views
-3

我正在使用父組件內的子組件。如下圖所示,有關組件渲染的反應警告

class Parent extends Component() { 
    getContent() { 
    let arr = [{name: 'abc'},{name: 'bcd'},{name: 'cde'}]; 
    return arr.map(data => <Child name={data.name}/>) 
    } 

    render() { 
    return (
     <div>{this.getContent()}</div> 
    ) 
    } 
} 

上面的代碼在警告

陣列中的每個孩子都應該有一個獨特的「鑰匙」的道具。檢查 渲染TableComponent

的方法,我不希望像答案使用子組件唯一鍵的道具。相反,請澄清爲什麼反應會給出這種類型的警告,如果我不在這裏使用唯一的密鑰,那將會是什麼缺點。請提供這個原因的例子。

+4

可能的重複[瞭解React.js中數組子元素的唯一鍵](https://stackoverflow.com/questions/28329382/understanding- unique-keys-for-array-children-in-react-js) – poke

+1

請參閱[第二最多投票回答](https://stackoverflow.com/a/43892905/216074)以獲得關於'key'用於什麼。 – poke

回答

0

鍵幫助React識別哪些項目已更改,添加或刪除。應該給數組中的元素賦予元素一個穩定的標識