Skip to content

Commit d8f9030

Browse files
author
bd-arc
committed
fix(Carousel): make sure that active item is updated even if no callback has been specified
1 parent ac2e144 commit d8f9030

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/carousel/Carousel.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class Carousel extends Component {
8585
this._positions = [];
8686
this._currentContentOffset = 0; // store ScrollView's scroll position
8787
this._canFireCallback = false; // used only when `enableMomentum` is set to `false`
88-
this._callbackScrollOffset = null; // used only when `enableMomentum` is set to `false`
88+
this._scrollOffsetRef = null; // used only when `enableMomentum` is set to `false`
8989

9090
this._getItemLayout = this._getItemLayout.bind(this);
9191
this._initPositionsAndInterpolators = this._initPositionsAndInterpolators.bind(this);
@@ -415,15 +415,15 @@ export default class Carousel extends Component {
415415

416416
if (enableMomentum) {
417417
clearTimeout(this._snapNoMomentumTimeout);
418-
} else if (this._canFireCallback &&
419-
activeItem !== nextActiveItem &&
418+
} else if (activeItem !== nextActiveItem &&
420419
nextActiveItem === this._itemToSnapTo &&
421-
(scrollOffset >= this._callbackScrollOffset - callbackOffsetMargin ||
422-
scrollOffset <= this._callbackScrollOffset + callbackOffsetMargin)) {
423-
this._canFireCallback = false;
424-
this.setState({ activeItem: nextActiveItem }, () => {
420+
(scrollOffset >= this._scrollOffsetRef - callbackOffsetMargin ||
421+
scrollOffset <= this._scrollOffsetRef + callbackOffsetMargin)) {
422+
this.setState({ activeItem: nextActiveItem });
423+
if (this._canFireCallback) {
424+
this._canFireCallback = false;
425425
this._onSnap(nextActiveItem);
426-
});
426+
}
427427
}
428428

429429
if (onScroll) {
@@ -634,6 +634,8 @@ export default class Carousel extends Component {
634634
this._onSnap(index);
635635
}
636636
} else {
637+
this._scrollOffsetRef = this._positions[index] && this._positions[index].start;
638+
637639
// 'scrollEndDrag' might be fired when "peaking" to another item. We need to
638640
// make sure that callback is fired when scrolling back to the right one.
639641
this._itemToSnapTo = index;
@@ -642,7 +644,6 @@ export default class Carousel extends Component {
642644
// Thus we need a flag to make sure that it's going to be called only once.
643645
if (onSnapToItem && fireCallback) {
644646
this._canFireCallback = true;
645-
this._callbackScrollOffset = this._positions[index] && this._positions[index].start;
646647
}
647648
}
648649

0 commit comments

Comments
 (0)