Ken428965 2022-11-03 12:54 采纳率: 53.6%
浏览 50
已结题

react,housedetail里的浏览器报错Each child in a list should have a unique "key" prop?

在使用的react版本如下:

img


已写的部分代码:

// ...
export default class HouseDetail extends Component {
// ...
// 渲染轮播图结构
  renderSwipers() {
    const {
      houseInfo: { houseImg }
    } = this.state

        return houseImg.map(item => 
      (<Swiper.Item>
        <a  key={item} href="https://www.baidu.com">
          <img src={BASE_URL + item} alt="" />
        </a>
      </Swiper.Item>
    ))
  }
// ...
// 渲染标签
  renderTags() {
    const {
      houseInfo: { tags }
    } = this.state

    return tags.map((item, index) => {
      let tagClass = ''
      if (index > 2) {
        tagClass = 'tag3'
      } else {
        tagClass = 'tag' + (index + 1)
      }

      return (
        <span key={item} className={[styles.tag, styles[tagClass]].join(' ')}>
          {item}
        </span>
      )
    })
  }
// ...
<div className={styles.items}>
            {recommendHouses.map(item => (
              <HouseItem {...item} key={item.id} />
            ))}
          
// ...

chrome浏览器报错:

img


代码里的每一个key都是唯一的,请问为何仍报错说需要唯一的key?请问如何解决?望能对现有代码进行举例说明。

  • 写回答

1条回答 默认 最新

  • 崽崽的谷雨 2022-11-03 13:32
    关注

    key={item} 加到 swiper-item 标签上呢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月7日
  • 已采纳回答 11月3日
  • 创建了问题 11月3日

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作