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" >
                                    
                                        "100%" height="100%" />
                                    
                                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}>
                                    
                                        "100%" height="100%" />
                                    
                                div>
                            )
                        })
                    }
                div>
                <div className="swiper-pagination">div>
            div>
        )

    }
}

你可能感兴趣的:(React)