简体   繁体   English

react-slick slickNext方法 - “未捕获的TypeError:无法读取未定义的属性'滑块'”

[英]react-slick slickNext method - “Uncaught TypeError: Cannot read property 'slider' of undefined”

I'm using the react-slick carousel with the Green Sock animation tool, and I've gotten the slider and animations to work. 我正在使用带有Green Sock动画工具的反应光滑旋转木马,我已经让滑块和动画工作了。 The problem now is calling slickNext method when the animation ends on a slide. 现在的问题是当动画在幻灯片上结束时调用slickNext方法。 I keep getting this error whether I use refs or not (but my "test" is working and showing in the console). 无论我是否使用refs,我都会收到此错误(但我的“测试”正在工作并在控制台中显示)。 The github documentation for this method is a bit meager and confusing, and I haven't found any similar situations to mine that I can use as reference. 这个方法的github文档有点微薄和混乱,我没有发现任何类似的情况,我可以用作参考。 How can I access this method? 我该如何访问此方法?

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>;
  }
}

Bind your method to ensure the context this is taken care of: 绑定你的方法,以确保上下文this是照顾:

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

When you just pass, var timeline1 = new TimelineLite({onComplete: this.nextSlide}) you are effectively just concentrating on passing the callback, but the method nextSlide uses the this context and when the callback is invoked, you need to ensure that this has the refs in them to invoke the slider . 当你刚刚传递时, var timeline1 = new TimelineLite({onComplete: this.nextSlide})你实际上只是集中精力传递回调,但是nextSlide方法使用了this上下文,当调用回调时,你需要确保thisrefs在他们调用slider Read more here 在这里阅读更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 react-slick 轮播中执行 slickNext 方法? - How to execute slickNext method in react-slick carousel? React-slick slider 测试用例错误 - 无法读取属性'querySelectorAll' - React-slick slider test case error - cannot read property 'querySelectorAll' 反应-未捕获的TypeError:无法读取未定义的属性“ 1” - React - Uncaught TypeError: Cannot read property '1' of undefined 未捕获的类型错误:无法读取图像 slider 的未定义属性“样式” - Uncaught TypeError: Cannot read property 'style' of undefined for image slider Facebook 滑块:未捕获的类型错误:无法读取未定义的属性“长度” - Facebook Slider: Uncaught TypeError: Cannot read property 'length' of undefined JQuery UI Slider Uncaught TypeError:无法读取未定义的属性“addClass” - JQuery UI Slider Uncaught TypeError: Cannot read property 'addClass' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;setData&#39;[Slider] - Uncaught TypeError: Cannot read property 'setData' of undefined[Slider] W3 Slider - 未捕获的类型错误:无法读取未定义的属性“类名” - W3 Slider - Uncaught TypeError: Cannot read property 'className' of undefined 反应:未捕获的TypeError:无法读取未定义的属性&#39;setState&#39; - React: Uncaught TypeError: Cannot read property 'setState' of undefined React Apollo:未捕获(承诺)类型错误:无法读取未定义的属性“重新获取” - React Apollo: Uncaught (in promise) TypeError: Cannot read property 'refetch' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM