Skip to content

Commit

Permalink
Merge pull request #539 from flyskywhy/patch-1
Browse files Browse the repository at this point in the history
Fix `Uncaught TypeError: _this.refs.scrollView.scrollTo is not a function`
  • Loading branch information
Bart Arribe authored Sep 11, 2017
2 parents 9facd28 + adee859 commit ead66e5
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export default class extends Component {
loopJump = () => {
if (!this.state.loopJump) return
const i = this.state.index + (this.props.loop ? 1 : 0)
const scrollView = this.refs.scrollView
const scrollView = this.scrollView
this.loopJumpTimer = setTimeout(() => scrollView.setPageWithoutAnimation &&
scrollView.setPageWithoutAnimation(i), 50)
}
Expand Down Expand Up @@ -425,10 +425,10 @@ export default class extends Component {
if (state.dir === 'x') x = diff * state.width
if (state.dir === 'y') y = diff * state.height

if (Platform.OS === 'android') {
this.refs.scrollView && this.refs.scrollView[animated ? 'setPage' : 'setPageWithoutAnimation'](diff)
if (Platform.OS !== 'ios') {
this.scrollView && this.scrollView[animated ? 'setPage' : 'setPageWithoutAnimation'](diff)
} else {
this.refs.scrollView && this.refs.scrollView.scrollTo({ x, y, animated })
this.scrollView && this.scrollView.scrollTo({ x, y, animated })
}

// update scroll state
Expand All @@ -438,7 +438,7 @@ export default class extends Component {
})

// trigger onScrollEnd manually in android
if (!animated || Platform.OS === 'android') {
if (!animated || Platform.OS !== 'ios') {
setImmediate(() => {
this.onScrollEnd({
nativeEvent: {
Expand Down Expand Up @@ -579,10 +579,14 @@ export default class extends Component {
)
}

refScrollView = view => {
this.scrollView = view;
}

renderScrollView = pages => {
if (Platform.OS === 'ios') {
return (
<ScrollView ref='scrollView'
<ScrollView ref={this.refScrollView}
{...this.props}
{...this.scrollViewPropOverrides()}
contentContainerStyle={[styles.wrapperIOS, this.props.style]}
Expand All @@ -595,7 +599,7 @@ export default class extends Component {
)
}
return (
<ViewPagerAndroid ref='scrollView'
<ViewPagerAndroid ref={this.refScrollView}
{...this.props}
initialPage={this.props.loop ? this.state.index + 1 : this.state.index}
onPageSelected={this.onScrollEnd}
Expand Down

0 comments on commit ead66e5

Please sign in to comment.