解決React報錯Encountered?two?children?with?the?same?key
總覽
當(dāng)我們從map()
方法返回的兩個或兩個以上的元素具有相同的key
屬性時,會產(chǎn)生"Encountered two children with the same key"錯誤。為了解決該錯誤,為每個元素的key
屬性提供獨一無二的值,或者使用索引參數(shù)。
這里有個例子來展示錯誤是如何發(fā)生的。
// App.js const App = () => { // ??? name property is not a unique identifier const people = [ {id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Alice'}, ]; /** * ?? Encountered two children with the same key, `Alice`. * Keys should be unique so that components maintain their identity across updates. * Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version. */ return ( <div> {people.map(person => { return ( <div key={person.name}> <h2>{person.id}</h2> <h2>{person.name}</h2> </div> ); })} </div> ); }; export default App;
上述代碼片段的問題在于,我們在每個對象上使用name
屬性作為key
屬性,但是name
屬性在整個對象中不是獨一無二的。
index
解決該問題的一種方式是使用索引。它是傳遞給map
方法的第二個參數(shù)。
const App = () => { const people = [ {id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Alice'}, ]; // ??? now using index for key return ( <div> {people.map((person, index) => { return ( <div key={index}> <h2>{person.id}</h2> <h2>{person.name}</h2> </div> ); })} </div> ); }; export default App;
我們傳遞給Array.map
方法的函數(shù)被調(diào)用,其中包含了數(shù)組中的每個元素和正在處理的當(dāng)前元素的索引。
索引保證是唯一的,但是用它來做key
屬性并不是一個最好的做法。因為它不穩(wěn)定,在渲染期間會發(fā)生變化。
唯一標(biāo)識
更好的解決方案是,使用一個能唯一標(biāo)識數(shù)組中每個元素的值。
在上面的例子中,我們可以使用對象上的id
屬性,因為每個id
屬性保證是唯一的。
// App.js const App = () => { const people = [ {id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Alice'}, ]; // ? now using the id for the key prop return ( <div> {people.map(person => { return ( <div key={person.id}> <h2>{person.id}</h2> <h2>{person.name}</h2> </div> ); })} </div> ); }; export default App;
使用id
作為key
屬性好多了。因為我們保證了對象id
屬性為1
時,name
屬性總是等于Alice
。
React使用我們傳遞給key
屬性的值是出于性能方面的考慮,以確保它只更新在渲染期間變化的列表元素。
當(dāng)數(shù)組中每個元素都擁有獨一無二的key
時,React會更容易確定哪些列表元素發(fā)生了變化。
你可以使用index
作為key
屬性。然而,這可能會導(dǎo)致React在幕后做更多的工作,而不是像獨一無二的id
屬性那樣穩(wěn)定。
盡管如此,除非你在渲染有成千上萬個元素的數(shù)組,否則你很有可能不會注意到使用索引和唯一標(biāo)識符之間有什么區(qū)別。
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報錯Encountered two children with the same key的詳細內(nèi)容,更多關(guān)于React報錯same key的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決React報錯`value` prop on `input` should not be null
- react component changing uncontrolled input報錯解決
- 解決React報錯useNavigate()?may?be?used?only?in?context?of?Router
- 解決React報錯Style prop value must be an object
- 解決React報錯The?tag?is?unrecognized?in?this?browser
- 解決React報錯Cannot assign to 'current' because it is a read-only property
- 解決React報錯Functions are not valid as a React child
- 解決React報錯No duplicate props allowed
相關(guān)文章
webpack+react+antd腳手架優(yōu)化的方法
本篇文章主要介紹了webpack+react+antd腳手架優(yōu)化的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04React通過ref獲取子組件的數(shù)據(jù)和方法
這篇文章主要介紹了React如何通過ref獲取子組件的數(shù)據(jù)和方法,文中有詳細的總結(jié)內(nèi)容和代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-10-10react 不用插件實現(xiàn)數(shù)字滾動的效果示例
這篇文章主要介紹了react 不用插件實現(xiàn)數(shù)字滾動的效果示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04