Skip to content

Commit

Permalink
feat(Carousel): add method triggerRenderingHack() to work around a …
Browse files Browse the repository at this point in the history
…random FlatList bug that keeps content hidden until the carousel is scrolled

See facebook/react-native#1831
  • Loading branch information
bd-arc committed Jan 3, 2018
1 parent 518543c commit 24731a0
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/carousel/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class Carousel extends Component {
this._canFireCallback = false;
this._scrollOffsetRef = null;
this._onScrollTriggered = true; // used when momentum is enabled to prevent an issue with edges items
this._lastScrollDate = 0; // used to work around a FlatList bug
this._scrollEnabled = props.scrollEnabled === false ? false : true;

this._initPositionsAndInterpolators = this._initPositionsAndInterpolators.bind(this);
Expand Down Expand Up @@ -169,7 +170,8 @@ export default class Carousel extends Component {

this._initPositionsAndInterpolators();

this._didMountTimeout = setTimeout(() => {
// Without 'requestAnimationFrame' or a `0` timeout, images will randomly not be rendered on Android...
requestAnimationFrame(() => {
this._snapToItem(_firstItem, false, false, true, false);
this._hackActiveSlideAnimation(_firstItem, 'start', true);

Expand All @@ -180,7 +182,7 @@ export default class Carousel extends Component {
} else {
apparitionCallback();
}
}, 0);
});
}

shouldComponentUpdate (nextProps, nextState) {
Expand Down Expand Up @@ -239,7 +241,6 @@ export default class Carousel extends Component {

componentWillUnmount () {
this.stopAutoplay();
clearTimeout(this._didMountTimeout);
clearTimeout(this._apparitionTimeout);
clearTimeout(this._hackSlideAnimationTimeout);
clearTimeout(this._enableAutoplayTimeout);
Expand Down Expand Up @@ -669,6 +670,7 @@ export default class Carousel extends Component {

this._currentContentOffset = scrollOffset;
this._onScrollTriggered = true;
this._lastScrollDate = Date.now();

if (this._activeItem !== nextActiveItem && this._shouldUseCustomAnimation()) {
this._playCustomSlideAnimation(this._activeItem, nextActiveItem);
Expand Down Expand Up @@ -992,6 +994,22 @@ export default class Carousel extends Component {
this._snapToItem(newIndex, animated);
}

// https://github.com/facebook/react-native/issues/1831#issuecomment-231069668
triggerRenderingHack (offset) {
// Avoid messing with user scroll
if (Date.now() - this._lastScrollDate < 500) {
return;
}

const scrollPosition = this._currentContentOffset;
if (!scrollPosition && scrollPosition !== 0) {
return;
}

const scrollOffset = offset || (scrollPosition === 0 ? 1 : -1);
this._scrollTo(scrollPosition + scrollOffset, false);
}

_getComponentOverridableProps () {
const {
enableMomentum,
Expand Down

0 comments on commit 24731a0

Please sign in to comment.