Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...

原组件:

//carousel.jsx
import React from 'react';


export default class CarouselList extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        var lists = this.props.lists;
        return (
            <div className="swiper-container">
                <div className="swiper-wrapper">
                    {
                        lists.map(function(data,i){
                            return (
                                <div className="swiper-slide" >
                                    <a href={data.url}>
                                        <img src={data.src} width="100%" height="100%" />
                                    </a>
                                </div>
                            )
                        })
                    }
                </div>
                <div className="swiper-pagination"></div>
            </div>
        )

    }
}

上面的组件执行会报错:Warning: Each child in an array or iterator should have a unique “key”
prop. Check the render method of CarouselList. See
https://fb.me/react-warning-keys for more information.

解决办法:循环的时候加个key={i} 虽然并没啥用,但是必须加

改后:

import React from 'react';


export default class CarouselList extends React.Component{
    constructor(props){
        super(props);
    }
    render(){
        var lists = this.props.lists;
        return (
            <div className="swiper-container">
                <div className="swiper-wrapper">
                    {
                        lists.map(function(data,i){
                            return (
                                <div className="swiper-slide" key={i}>
                                    <a href={data.url}>
                                        <img src={data.src} width="100%" height="100%" />
                                    </a>
                                </div>
                            )
                        })
                    }
                </div>
                <div className="swiper-pagination"></div>
            </div>
        )

    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值