光滑轮播错误

时间:2016-11-17 04:45:56

标签: javascript reactjs slick.js

有人知道这个错误的可能原因吗?

Uncaught TypeError: Cannot read property 'add' of null

我正在使用React.js的光滑轮播。我将图像数组作为道具传递,并调用图像滑块组件中的光滑功能。这个组件在一个页面上运行正常,但在不同的页面上我一直看到这个错误,图片显示在滑块中但我认为由于这个错误其他的东西在页面上有所突破,有什么想法吗?

下面是组件的主要代码,图像作为道具从父母传递:

import React from 'react';
import {Link} from 'react-router';
import _ from 'lodash';
import Carousel from '../../Util/Carousel';

const ResOpt = [{
    breakpoint: 768,
    settings: {
      slidesToShow: 3,
      slidesToScroll: 3,
      arrows: true,
      dots: false
    }
  }, {
    breakpoint: 600,
    settings: {
      slidesToShow: 1.5,
      slidesToScroll: 1.5,
      arrows: false,
      dots: false,
      infinite: false
    }
  }
];

class ImagesCarousel extends React.Component {
  constructor() {
    super();
    this.carouselDidStart = false;
    this.carousel = new Carousel({
      outerSelector: ".carousel-container",
      innerSelector: ".images-container",
      responsive: ResOpt,
      infinite: false
    });
  }

  componentDidUpdate() {
    if (!this.carouselDidStart && this.dataIsLoaded() ) {
      this.carousel.startCarousel();

    }
  }

  componentWillUnmount() {
    if (this.carousel) {
      this.carousel.destroy();
      this.carousel = null;
    }
  }

  dataIsLoaded() {
    return !_.isEmpty(this.props.images);
  }

  render() {

    let images = this.props.images;

    return (
      <div className="detail-images">

        <div className="carousel-container">
          <div className="images-container">
            {
              (images || []).map((image, index) => {
                return <div key={image.imageId} className="img-item"><img src={image.imageName}  /></div>
              })
            }
          </div>
        </div>
      </div>
    );
  }
}

export default ImagesCarousel;

然后下面是主要的轮播类:

import $ from 'jquery';
import slick from 'slick-carousel';


export default class Carousel {
  constructor(options) {
    this.carouselIsOn = false;
    this.breakpoint = breakpoint;
    this.innerSelector = options['innerSelector'];
    this.outerSelector = options['outerSelector'];
    this.slickOptions = {
      infinite: options.infinite || true,
      slidesToShow: options['slidesToShow'] || 3,
      slidesToScroll: options['slidesToScroll'] || 3,
      dots: true,
      arrows: true,
      appendArrows: options.appendArrows || `${this.outerSelector} .carousel-nav`,
      appendDots: options.appendDots  || `${this.outerSelector} .carousel-nav`,
      responsive: options.responsive || DEFAULT_RESPONSIVE
    };
    this.startCarousel = this.startCarousel.bind(this);
  }

  startCarousel() {
    const carouselContainer = $(this.innerSelector);
    carouselContainer.slick(this.slickOptions);
    this.carouselIsOn = true;

    carouselContainer.find('.slick-cloned').attr('data-reactid', null);
  }

  destroy() {
    $(this.innerSelector).slick("unslick");
  }


}

1 个答案:

答案 0 :(得分:0)

这可能是初始化光滑两次的结果 - 请参阅此other SO question。您可以使用init event检查是否正在运行init两次。

$( '.your-slick-container' ).on('init', function(event, slick, direction){
    console.log('slick init');
});
相关问题