react-slick slickNext method - "Uncaught TypeError: Cannot read property 'slider' of undefined"

react-slick slickNext method - "Uncaught TypeError: Cannot read property 'slider' of undefined"

我正在使用带有 Green Sock 动画工具的 react-slick 旋转木马,我已经让滑块和动画正常工作。现在的问题是当动画在幻灯片上结束时调用 slickNext 方法。无论我是否使用 refs,我都会不断收到此错误(但我的 "test" 正在运行并显示在控制台中)。该方法的 github 文档有点少且令人困惑,而且我还没有找到任何与我类似的情况可以作为参考。如何访问此方法?

import React, { Component } from "react";
import Helmet from "react-helmet";
import AnimationStory1 from "../../components/Animation/AnimationStory1";
import AnimationStory2 from "../../components/Animation/AnimationStory2";
import AnimationStory3 from "../../components/Animation/AnimationStory3";
var Slider = require("react-slick");

export default class IntroStory extends Component {

  constructor (props) {
    super(props);
    this.state = {};
  }

  nextSlide () {
    console.log("test");
    this.refs.slider.slickNext();
  }

  render () {
//These are GreenSock animation instances
    var timeline1 = new TimelineLite({onComplete: this.nextSlide});
    var timeline2 = new TimelineLite();
    var timeline3 = new TimelineLite();

//These are settings for the react-slick slider
    var settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1,
//      slide: true,
      swipe: true,
      accessibility: true,
      arrows: false,
      beforeChange: function () {
        timeline1.restart();
        timeline2.restart();
        timeline3.restart();
      }
    };

    return <div>
      <Helmet title="Welcome to We Vote" />
      <div className="container-fluid well u-gutter__top--small fluff-full1 intro-story">
        <Slider ref="slider" {...settings}>
          <div key={1}><AnimationStory1 timeline1={timeline1}/></div>
          <div key={2}><AnimationStory2 timeline2={timeline2}/></div>
          <div key={3}><AnimationStory3 timeline3={timeline3}/></div>
          <div key={4}><p>This will be an image</p></div>
         </Slider>
      </div>
    </div>;
  }
}

绑定您的方法以确保处理上下文 this

 render () {
//These are GreenSock animation instances
    var timeline1 = new TimelineLite({onComplete: this.nextSlide.bind(this)});
    var timeline2 = new TimelineLite();
    var timeline3 = new TimelineLite();

刚过去的时候, var timeline1 = new TimelineLite({onComplete: this.nextSlide}) 您实际上只是专注于传递回调,但是方法 nextSlide 使用 this 上下文,并且在调用回调时,您需要确保 this 具有 refs在其中调用 sliderRead more here