简体   繁体   English

每个子列表键都是唯一的,但我仍然在控制台中收到错误消息。 反应器

[英]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.

相关问题 列表中的每个孩子都应该有一个唯一的“关键”道具控制台错误 - Each child in a list should have a unique "key" prop console error 列表中的每个孩子都应该有一个唯一的“关键”道具。 我做到了,但仍然看到这个错误 - Each child in a list should have a unique "key" prop. I made it, but still see this error 列表中的每个孩子都应该有一个唯一的“关键”道具,我在我的反应原生应用程序中遇到了这个错误 - Each child in a list should have a unique "key" prop, I am getting this error in my react native app 列表中的每个孩子都应该有一个唯一的“钥匙”道具 Reactjs - Each child in a list should have a unique "key" prop Reactjs Reactjs:警告列表中的每个孩子都应该有一个唯一的“关键”道具 - Reactjs: Warning Each child in a list should have a unique "key" prop ReactJs-列表中的每个孩子应该有一个唯一的“关键”道具 - ReactJs - Each child in a list should have a unique “key” prop 控制台错误:index.js:1 警告:列表中的每个孩子都应该有一个唯一的“key”道具 - Console error: index.js:1 Warning: Each child in a list should have a unique “key” prop 在控制台中出现此错误列表中的每个孩子都应该有一个唯一的“关键”道具 - Having this error in the console Each child in a list should have a unique “key” prop 列表中的每个孩子都应该有一个独特的“关键”道具。 即使在分配密钥后仍然如此 - Each child in a list should have a unique "key" prop. still even after assgining a key 错误警告:列表中的每个孩子都应该有一个唯一的“关键”道具 - ERROR Warning: Each child in a list should have a unique "key" prop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM