[英]Each child list key is unique yet I still get the error in my console. Reactjs
I keep getting Each child in a list should have a unique "key" prop
even though patient.timestamp and date is unique.我不断让
Each child in a list should have a unique "key" prop
即使 Patient.timestamp 和 date 是唯一的。 obsHistory is an array and date is a string and date is only render once in a table row td. obsHistory 是一个数组,日期是一个字符串,日期仅在表行 td 中呈现一次。 when I take out
obsHistory.map
or the other <tr key={date}
one at a time the error still persists.当我一次取出
obsHistory.map
或另一个<tr key={date}
时,错误仍然存在。 I can't get the key prop error resolved not sure why.我无法解决关键道具错误,不知道为什么。
renderObsHistory() {
const patientDetails = this.state.observations;
let groupedByDate = patientDetails.reduce((groupArray, patient) => {
groupArray[patient.date] = groupArray[patient.date] || [];
groupArray[patient.date].push(patient);
return groupArray;
}, {});
return Object.entries(groupedByDate).map(([date, obsHistory]) => (
<>
<tr key={date}>
<td>
<strong>{date}</strong>
</td>
</tr>
{obsHistory.map(patient => (
<tr key={patient.timestamp}>
<td>{patient.timestamp}</td>
</tr>
))}
</>
));
}
You should put the key on the Fragment ( <>
).您应该将密钥放在 Fragment (
<>
) 上。
Like this :像这样 :
<React.Fragment key={data}>
<tr>
/* ... */
</tr>
/* ... */
</React.Fragment>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.