laurenchen0631 / react-owl-carousel

React + Owl Carousel
Apache License 2.0
198 stars 109 forks source link

Every state change slider re-rendering and back to first slide. #34

Open sahid-webhawksit opened 6 years ago

sahid-webhawksit commented 6 years ago

When I want to change the state in my component its re-render the slider again and again. How can I fixed this issue ?

ghost commented 6 years ago

Can no one solve this problem?

iamtomhanks commented 6 years ago

Having same issue.

iamtomhanks commented 6 years ago

I've solved for just dragging the carousel by doing the following:

  1. store the startPosition in state: this.state = { startPosition: '0' };

  2. Pass startPosition to OwlCarousel as prop startPosition={this.state.startPosition}

  3. Pass this as well as a prop: onDragged={(object) => this.updateCarouselPosition(object)}

  4. Function:

updateCarouselPosition(object){ if(object.item.index != this.state.startPosition){ this.setState({ startPosition: object.item.index }); } }

akon3000 commented 6 years ago

How to fix step by step -:

  1. Pass ref={(el) => { this.slider = el; }} to OwlCarousel as prop

  2. Inside function componentDidUpdate set componentDidUpdate() { this.slider.owlCarousel.trigger('refresh.owl.carousel'); }

3.Good luck.

mxdi9i7 commented 6 years ago

Problem fixed with iamtomhanks's solution. You do need to manually keep count of the startPosition to avoid resetting the index everytime onChange is triggered on owlCarousel.

truongnm97 commented 5 years ago

I've solved for just dragging the carousel by doing the following:

  1. store the startPosition in state: this.state = { startPosition: '0' };
  2. Pass startPosition to OwlCarousel as prop startPosition={this.state.startPosition}
  3. Pass this as well as a prop: onDragged={(object) => this.updateCarouselPosition(object)}
  4. Function:

updateCarouselPosition(object){ if(object.item.index != this.state.startPosition){ this.setState({ startPosition: object.item.index }); } }

nice, but your solution doesn't work with infinite loop slideshow

moxon6 commented 4 years ago
  const startPosition = useRef(0);
  return (
    <OwlCarousel
      startPosition={startPosition.current}
      onDragged={({ item, page }) => {
        startPosition.current = item.index - page.size;
      }}
    >
        ...
    </OwlCarousel>
    )
Henry-Bot-Hive commented 2 years ago

anyone know how to get this working for Angular?

mariomurrent-softwaresolutions commented 1 year ago

I've solved for just dragging the carousel by doing the following:

  1. store the startPosition in state: this.state = { startPosition: '0' };
  2. Pass startPosition to OwlCarousel as prop startPosition={this.state.startPosition}
  3. Pass this as well as a prop: onDragged={(object) => this.updateCarouselPosition(object)}
  4. Function:

updateCarouselPosition(object){ if(object.item.index != this.state.startPosition){ this.setState({ startPosition: object.item.index }); } }

But this causes some flickering

raghav1086 commented 1 week ago

@iamtomhanks solutions works for me.