diff --git a/src/ScrollPager.tsx b/src/ScrollPager.tsx index 16eae4ef..f39cd304 100644 --- a/src/ScrollPager.tsx +++ b/src/ScrollPager.tsx @@ -85,7 +85,15 @@ export default class ScrollPager extends React.Component< private scrollTo = (x: number, animated = true) => { if (this.scrollViewRef.current) { - this.scrollViewRef.current.getNode().scrollTo({ + // getNode() is not necessary in newer versions of React Native + const scrollView = + // @ts-ignore + typeof this.scrollViewRef.current?.scrollTo === 'function' + ? this.scrollViewRef.current + : this.scrollViewRef.current?.getNode(); + + // @ts-ignore + scrollView?.scrollTo({ x, animated: animated, }); diff --git a/src/TabBar.tsx b/src/TabBar.tsx index 586dae2c..195a0c92 100644 --- a/src/TabBar.tsx +++ b/src/TabBar.tsx @@ -2,7 +2,6 @@ import * as React from 'react'; import { StyleSheet, View, - ScrollView, StyleProp, ViewStyle, TextStyle, @@ -139,7 +138,7 @@ export default class TabBar extends React.Component< private scrollAmount = new Animated.Value(0); - private scrollView: ScrollView | undefined; + private scrollViewRef = React.createRef(); private cancelNextFrameCb: (() => void) | undefined = undefined; @@ -270,11 +269,18 @@ export default class TabBar extends React.Component< private resetScroll = (index: number) => { if (this.props.scrollEnabled) { - this.scrollView && - this.scrollView.scrollTo({ - x: this.getScrollAmount(this.props, this.state, index), - animated: true, - }); + // getNode() is not necessary in newer versions of React Native + const scrollView = + // @ts-ignore + typeof this.scrollViewRef.current?.scrollTo === 'function' + ? this.scrollViewRef.current + : this.scrollViewRef.current?.getNode(); + + // @ts-ignore + scrollView?.scrollTo({ + x: this.getScrollAmount(this.props, this.state, index), + animated: true, + }); } }; @@ -413,10 +419,7 @@ export default class TabBar extends React.Component< }, }, ])} - ref={(el) => { - // @ts-ignore - this.scrollView = el?.getNode(); - }} + ref={this.scrollViewRef} > {routes.map((route: T) => { const props: TabBarItemProps & { key: string } = {