Skip to content

Commit

Permalink
Remove flickering when initialPage is used 🎨 🐛
Browse files Browse the repository at this point in the history
  • Loading branch information
Valery Buchinsky committed Oct 5, 2017
1 parent ac8c74f commit 614d211
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/libraries/ViewPager/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import React, { PropTypes, PureComponent } from 'react';
import { View, FlatList, ViewPropTypes, InteractionManager } from 'react-native';
import {
View,
FlatList,
ViewPropTypes,
InteractionManager,
Dimensions
} from 'react-native';
import Scroller from 'react-native-scroller';
import { createResponder } from 'react-native-gesture-responder';

const MIN_FLING_VELOCITY = 0.5;

// Dimensions are only used initially.
// onLayout should handle orientation swap.
const {width, height} = Dimensions.get('window');

export default class ViewPager extends PureComponent {
static propTypes = {
...View.propTypes,
Expand Down Expand Up @@ -38,8 +48,8 @@ export default class ViewPager extends PureComponent {
gestureResponder = undefined;

state = {
width: 0,
height: 0
width,
height,
}

constructor (props) {
Expand Down Expand Up @@ -90,7 +100,16 @@ export default class ViewPager extends PureComponent {
}

componentDidMount () {
this.scrollToPage(this.props.initialPage, true);
// FlatList is set to render at initialPage.
// The scroller we use is not aware of this.
// Let it know by simulating most of what happens in scrollToPage()
this.onPageScrollStateChanged('settling');

const page = this.validPage(this.props.initialPage);
this.onPageChanged(page);

const finalX = this.getScrollOffsetOfPage(page);
this.scroller.startScroll(this.scroller.getCurrX(), 0, finalX - this.scroller.getCurrX(), 0, 0);
}

componentDidUpdate (prevProps) {
Expand Down

0 comments on commit 614d211

Please sign in to comment.