slickPause doesn't work

See original GitHub issue

I’m trying to use the new feature slickPause. However it doesn’t work. Before this feature I was changing an autoPlay state variable, so that the autoplay prop change to false when the slider has to pause.

// this.state.autoPlay is true by default 
pauseSlider() {
    this.setState({autoPlay: false});
}

...

render() {
    const {autoPlay} = this.state;
    const settings = {
        autoplaySpeed: 5000,
        className: 'swiper-wrapper',
        infinite: true,
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        autoplay: autoPlay,
        pauseOnHover: false,
        initialSlide: 1,
        beforeChange: this.beforeChangeSlide,
        afterChange: this.afterChangeSlide
    };

    ...
}

but the problem was that autoPlay to false made to slide one more time (because the autoPlayTimer was not cleared). Now, I expect the timer to be cleared when calling slickPause(), but it seems to do nothing, as it works the same way as not calling it. I’ve tried several ways:

Keeping the above example and calling this.slider.slickPause() from componentDidUpdate():

componentDidUpdate(prevProps, prevState) {
    const {autoPlay} = this.state;
    if (!autoPlay && prevState.autoPlay) {
        this.slider.slickPause();
    }
    // also tried calling `this.slider.slickPlay()` when `autoPlay && !prevState.autoPlay`
}

The same as above (with componentDidUpdate) but keeping autoplay prop to true:

render() {
    const {autoPlay} = this.state;
    const settings = {
        autoplaySpeed: 5000,
        className: 'swiper-wrapper',
        infinite: true,
        slidesToShow: 1,
        slidesToScroll: 1,
        arrows: false,
        autoplay: true, // ***
        pauseOnHover: false,
        initialSlide: 1,
        beforeChange: this.beforeChangeSlide,
        afterChange: this.afterChangeSlide
    };

    ...
}

In this way, it just doesn’t stop sliding.

Calling this.slider.slickPause() instead of change state

pauseSlider() {
    this.slider.slickPause();
}

Although I need to change the state also for other functionality so actually I change the state:

pauseSlider() {
    this.slider.slickPause();
    this.setState({foo: 'bar'});
}

Am I doing something wrong or is it a bug?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
laveesinghcommented, Feb 22, 2018

I tried replicating it and I found the problem. The autoplay prop was available in props instead of this.props in update mixin. Fixed that, fixed the issue. 817cd797b04e158a70ada4b2f1ea9c2dc98aab9a

The following snippet works for me:

import React, { Component } from 'react'
import Slider from '../src/slider'

export default class SimpleSlider extends Component {

  constructor(props) {
    super(props)
    this.state = {
      autoplay: false
    }
  }

  render() {
    const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1,
      autoplaySpeed: 500,
      autoplay: this.state.autoplay
    };
    return (
      <div>
        <h2> Single Item</h2>
        <div>
          <button onClick={() => this.setState({ autoplay: !this.state.autoplay })}> {this.state.autoplay ? 'Playing' : 'Paused' } </button>
        </div>
        <Slider {...settings}>
          <div><h3>1</h3></div>
          <div><h3>2</h3></div>
          <div><h3>3</h3></div>
          <div><h3>4</h3></div>
          <div><h3>5</h3></div>
          <div><h3>6</h3></div>
        </Slider>
      </div>
    );
  }
}
0reactions
laveesinghcommented, Mar 21, 2018

@msalsas Finally, I’ve managed to solved issues related to autoplay/pause by managing another state in InnerSlider component. Commits like: 466e026, 877c465 and bb5de6a solve the problem. These changes are currently in dev branch and will be released soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

slickPause() not working · Issue #738 · kenwheeler/slick - GitHub
i solved the problem finally! It's better to pause the slick animation with slick('slickpause') on mouse over, and then you can run it...
Read more >
javascript - Slick Init - slickPause undefined - Stack Overflow
When trying to pause the slider within the init listener the slick('slickPause') method doesn't work with the error.
Read more >
kenwheeler/slick - Gitter
Hi, I have some problem of speed with firefox... It's extremely slow... anyone had the same problem?
Read more >
Support Accessible Slick [#3196529] | Drupal.org
It doesn't seem to be doing any harm -- the settings work when I add them to the optionset (e.g., I can save...
Read more >
Play/Pause buttons for Slick Slider - CodePen
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https. ... .slick('slickPause')....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found