我正在使用带有Green Sock动画工具的反应滑动旋转木马,并且我已经让滑块和动画工作了。现在的问题是当动画在幻灯片上结束时调用slickNext方法。无论我是否使用refs,我都会收到这个错误(但是我的"测试"正在工作并在控制台中显示)。这种方法的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>;
}
}
答案 0 :(得分:0)
绑定您的方法以确保上下文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
在他们中调用slider
。 Read more here