[英]React.js, Each child in a list should have a unique "key" prop. how to fix if key is already given
Im getting the warning Warning: Each child in a list should have a unique "key" prop.
我收到警告
Warning: Each child in a list should have a unique "key" prop.
. . My understanding of react was that when you pass to a new component you give it the Id of a prop, which i am doing, yet im still getting this warning.
我对反应的理解是,当你传递给一个新组件时,你给它一个道具的 ID,我正在这样做,但我仍然收到这个警告。 In my code i have some dummy data that is used to set the state of table data ie
在我的代码中,我有一些用于设置表数据状态的虚拟数据,即
const DumData =
{ id: 1,
UserGroup:[
{
id: "1",
Name: "Dax Johnson",
AddressLine: "82 Mahu road",
Email: 'DaxIng@Gmail.co.nz',
ContactNumber: "02791743",
Zip: '8801',
Notes: 'His dog is swag',
Animals: [
{ id: "1",
PatientID: "23",
AnimalName: 'SwagDog',
Species: "Canine",
Breed: "Dog",
Gender: "Male",
DOB: "",
Vists: [{
id:1 ,
time: 'October 31st 2021'
},
{
id:2 ,
time: 'October 21st 2021'
}]
},
{ id: '2',
AnimalName: 'CoolCat',
Species: "Feline",
Breed: "Cat",
Gender: "Female",
DOB: "",
Vists: [{
id:1 ,
time: 'March 4th 2021'
}]
}
],
},
{
id: "12",
Name: "Willam McDonald",
AddressLine: "2 Wacky Ave",
Email: 'WILLIAM@hotmail.co.nz',
Zip: '8661',
ContactNumber: "033777300",
Notes: 'His cat is cool',
Animals: [
{
id: "1",
PatientID: "23",
AnimalName: "Molder",
Species: "Feline",
Breed: "Cat",
Gender: "Female",
DOB: "2008",
Vists: [{
id:1 ,
time: 'February 4th 2022'
}]
}
],
},
{
id: "3",
Name: "Oscar Issac",
AddressLine: "2 Wacky Ave",
Email: 'Oscar@hotmail.co.nz',
Zip: '7041',
ContactNumber: "0279000",
Notes: 'His cat is cool',
Animals: [
{
id: "1",
PatientID: "23",
AnimalName: "Cool cat",
Species: "Feline",
Breed: "Cat",
Gender: "Female",
DOB: "2008",
Vists: [{
id:1 ,
time: 'June 24th 2021'
}]
}
],
} ]
};
and then later const [tableData, settableData] = useState(DumData);
然后
const [tableData, settableData] = useState(DumData);
I create a component table called Hometable where i pass it the tableData and the key id我创建了一个名为 Hometable 的组件表,我将 tableData 和密钥 ID 传递给它
<div className='Hometable-div'>
<Hometable
data={tableData}
key={tableData.id}
></Hometable>
</div>
and then i map the data so it is displayed in the table in the Hometable component.然后我映射数据,使其显示在 Hometable 组件的表中。 like so
像这样
function Hometable(props) {
var OwnerName;
var Animalname;
var breed;
return (
<div className='table-container'>
<table>
<thead>
<tr>
<th>Owner</th>
<th>Animal Name</th>
<th>Type/Breed</th>
<th>Vist Time</th>
</tr>
</thead>
<tbody>
{props.data.UserGroup.map((person) => (
OwnerName = person.Name,
person.Animals.map((Animal) => (
Animalname = Animal.AnimalName,
breed = Animal.Breed,
Animal.Vists.map((vist) => (
<tr>
<td> <i class="bi bi-person-fill"></i> {OwnerName} </td>
<td> {Animalname}</td>
<td> {breed} </td>
<td> {vist.time} </td>
</tr>
))
))
))}
<tr>
<td className='footer'>
</td>
<td className='footer'>
</td>
<td className='footer'>
</td>
<td className='footer'>
<button className='TableButton'> Page 1</button>
</td>
</tr>
</tbody>
</table>
</div>
);
}
export default Hometable;
I understand i dont use the key value in Hometable
so this might be an easy fix if anyone can help me resolve this warning?我知道我不使用
Hometable
中的键值,所以如果有人可以帮助我解决此警告,这可能是一个简单的解决方法?
Try passing the key here in the code尝试在代码中传递密钥
{props.data.UserGroup.map((person) => (
OwnerName = person.Name,
person.Animals.map((Animal) => (
Animalname = Animal.AnimalName,
breed = Animal.Breed,
Animal.Vists.map((vist, index) => (
// or visit.id if available
<tr key={index}>
<td> <i class="bi bi-person-fill"></i> {OwnerName} </td>
<td> {Animalname}</td>
<td> {breed} </td>
<td> {vist.time} </td>
</tr>
))
))
))}
It's recommended to use keys coming from the data source such as visit.id.建议使用来自数据源的键,例如 visit.id。 Last resort should be using index.
最后的手段应该是使用索引。 For more information you can read here .
有关更多信息,您可以在此处阅读。
Try using this code :尝试使用此代码:
function Hometable(props) {
var OwnerName;
var Animalname;
var breed;
return (
<div className='table-container'>
<table>
<thead>
<tr>
<th>Owner</th>
<th>Animal Name</th>
<th>Type/Breed</th>
<th>Vist Time</th>
</tr>
</thead>
<tbody>
{props.data.UserGroup.map((person) => (
OwnerName = person.Name,
person.Animals.map((Animal) => (
Animalname = Animal.AnimalName,
breed = Animal.Breed,
Animal.Vists.map((vist,index) => (
<tr key={index} >
<td> <i class="bi bi-person-fill"></i> {OwnerName} </td>
<td> {Animalname}</td>
<td> {breed} </td>
<td> {vist.time} </td>
</tr>
))
))
))}
<tr>
<td className='footer'>
</td>
<td className='footer'>
</td>
<td className='footer'>
</td>
<td className='footer'>
<button className='TableButton'> Page 1</button>
</td>
</tr>
</tbody>
</table>
</div>
);
}
export default Hometable;
In my opinion:在我看来:
const array = [...];
...
const new_array_for_props = array.map(function(arr, index){
<target_comp props_01={arr.props_01} key={index} />
});
sub component(target_comp) unnecessary uses props.key.子组件(target_comp)不必要地使用 props.key。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.