解決React報(bào)錯(cuò)Encountered?two?children?with?the?same?key
總覽
當(dāng)我們從map()
方法返回的兩個(gè)或兩個(gè)以上的元素具有相同的key
屬性時(shí),會產(chǎn)生"Encountered two children with the same key"錯(cuò)誤。為了解決該錯(cuò)誤,為每個(gè)元素的key
屬性提供獨(dú)一無二的值,或者使用索引參數(shù)。
這里有個(gè)例子來展示錯(cuò)誤是如何發(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;
上述代碼片段的問題在于,我們在每個(gè)對象上使用name
屬性作為key
屬性,但是name
屬性在整個(gè)對象中不是獨(dú)一無二的。
index
解決該問題的一種方式是使用索引。它是傳遞給map
方法的第二個(gè)參數(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ù)組中的每個(gè)元素和正在處理的當(dāng)前元素的索引。
索引保證是唯一的,但是用它來做key
屬性并不是一個(gè)最好的做法。因?yàn)樗环€(wěn)定,在渲染期間會發(fā)生變化。
唯一標(biāo)識
更好的解決方案是,使用一個(gè)能唯一標(biāo)識數(shù)組中每個(gè)元素的值。
在上面的例子中,我們可以使用對象上的id
屬性,因?yàn)槊總€(gè)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
屬性好多了。因?yàn)槲覀儽WC了對象id
屬性為1
時(shí),name
屬性總是等于Alice
。
React使用我們傳遞給key
屬性的值是出于性能方面的考慮,以確保它只更新在渲染期間變化的列表元素。
當(dāng)數(shù)組中每個(gè)元素都擁有獨(dú)一無二的key
時(shí),React會更容易確定哪些列表元素發(fā)生了變化。
你可以使用index
作為key
屬性。然而,這可能會導(dǎo)致React在幕后做更多的工作,而不是像獨(dú)一無二的id
屬性那樣穩(wěn)定。
盡管如此,除非你在渲染有成千上萬個(gè)元素的數(shù)組,否則你很有可能不會注意到使用索引和唯一標(biāo)識符之間有什么區(qū)別。
原文鏈接:bobbyhadz.com/blog/react-…
以上就是解決React報(bào)錯(cuò)Encountered two children with the same key的詳細(xì)內(nèi)容,更多關(guān)于React報(bào)錯(cuò)same key的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決React報(bào)錯(cuò)`value` prop on `input` should not be null
- react component changing uncontrolled input報(bào)錯(cuò)解決
- 解決React報(bào)錯(cuò)useNavigate()?may?be?used?only?in?context?of?Router
- 解決React報(bào)錯(cuò)Style prop value must be an object
- 解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser
- 解決React報(bào)錯(cuò)Cannot assign to 'current' because it is a read-only property
- 解決React報(bào)錯(cuò)Functions are not valid as a React child
- 解決React報(bào)錯(cuò)No duplicate props allowed
相關(guān)文章
React項(xiàng)目中axios的封裝與API接口的管理詳解
Axios是一個(gè)npm軟件包,允許應(yīng)用程序?qū)TTP請求發(fā)送到Web API,下面這篇文章主要給大家介紹了關(guān)于React項(xiàng)目中axios的封裝與API接口的管理的相關(guān)資料,需要的朋友可以參考下2021-09-09webpack+react+antd腳手架優(yōu)化的方法
本篇文章主要介紹了webpack+react+antd腳手架優(yōu)化的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04React通過ref獲取子組件的數(shù)據(jù)和方法
這篇文章主要介紹了React如何通過ref獲取子組件的數(shù)據(jù)和方法,文中有詳細(xì)的總結(jié)內(nèi)容和代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10react 不用插件實(shí)現(xiàn)數(shù)字滾動(dòng)的效果示例
這篇文章主要介紹了react 不用插件實(shí)現(xiàn)數(shù)字滾動(dòng)的效果示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04詳解如何封裝和使用一個(gè)React鑒權(quán)組件
JavaScript?和?React?提供了靈活的事件處理機(jī)制,特別是通過利用事件的捕獲階段來阻止事件傳播可以實(shí)現(xiàn)精細(xì)的權(quán)限控制,本文將詳細(xì)介紹這一技術(shù)的應(yīng)用,并通過實(shí)踐案例展示如何封裝和使用一個(gè)?React?鑒權(quán)組件,需要的可以參考下2024-03-03