Slick Carousel Error

帅比萌擦擦* 提交于 2019-12-12 03:46:41

问题


Anyone knows possible causes of this error?

Uncaught TypeError: Cannot read property 'add' of null

I am using slick carousel with React.js. I pass in the images array as a prop and the call the slick function in the image slider component. This component works fine on one page but on a different page I keep seeing this error, images are showing in the slider but I think because of this error other things are breaking on the page, any ideas?

Below is the main code for the component, images are passed as props from the parent:

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;

And then below is the main carousel class:

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:


This might be the result of initialising slick twice - see this other SO question. You could check if you are running init twice with the init event.

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


来源:https://stackoverflow.com/questions/40646907/slick-carousel-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!